summaryrefslogtreecommitdiffstats
path: root/spec/board_spec.rb
blob: 0499778a02b94a5b193d13695ee40b6181361b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#

require 'spec_helper'
#
describe Iotas::Board do
    #
    before (:all) do
        @spin = Iotas::Spin.new 'dom0'
    end
    #
    before(:each) do
        @spin.clear!
    end
    #
    it "require_p release_p" do
        board = Iotas::Board.new 'hell', @spin
        p0 = board.require_p Iotas::Particle
        p1 = board.require_p Iotas::Particle
        (p0===p1).should be_false
        board.release_p p0
        p2 = board.require_p Iotas::Particle
        (p0===p2).should be_true
    end
    #
    it "particle wait and merge" do
        p0 = Iotas::Particle.new
        p0['k0'] = 'v0'
        p0['k1'] = 'neither'
        p0['k2'] = 'v2'
        p0.set_link_fields 'k0,k2'
        p0.link_value.should eql 'v0v2'
        p1 = Iotas::Particle.new
        p1['k0'] = 'v0'
        p1['k1'] = 'nore'
        p1['k2'] = 'v2'
        p1.set_link_fields 'k0,k2'
        p1.link_value.should eql 'v0v2'
        P0 = p0
        P1 = p1
        class Board0 < Iotas::Board
            attr_reader :ok, :follow
            def receive_p p
                @ok = false
                case p.action
                when Iotas::ACT_FOLLOW
                    @follow = true
                    @ok = (p===P0 and p.merged(0)===P1)
                else
                    @follow = false
                    @ok = (p===P1 and p.merged(0)===P0)
                end
            end
        end
        b0 = Board0.new 'door0', @spin
        b0.process_p p0
        p0.merged(0).should be_nil
        b0.process_p p1
        b0.ok.should be_true
        b0.follow.should be_false
        #
        p1.merged_shift
        #
        b0.process_p p0
        p0.merged(0).should be_nil
        # need to set it to p0 too, so case in Board0 is ok
        p0.add_dst Iotas::ACT_FOLLOW
        p0.split_dst!
        p1.add_dst Iotas::ACT_FOLLOW
        p1.split_dst!
        b0.process_p p1
        b0.ok.should be_true
        b0.follow.should be_true
    end
    #
    it "board->json->board" do
        board = Iotas::Board.new 'hell', @spin
        p0 = Iotas::Particle.new
        p1 = Iotas::Particle.new
        p1['v0']=0
        p1.set_link_fields 'v0'
        board.process_p p0
        board.process_p p1
        hell = Iotas::Board.json_create( JSON.load( JSON.generate(board) ) )
        board.name.should eql hell.name
        JSON.generate(board).should eql JSON.generate(hell)
    end
    #
end
#
# EOF