Example #1
0
func (p *Trie) GetDotString() string {
	rsrc := rand.NewSource(140209)
	rgen := rand.New(rsrc)

	outStr := "digraph Trie {"
	vec := new(vector.StringVector)

	p.outputDot(vec, 0, -1, rgen)

	cnt := vec.Len()
	for i := 0; i < cnt; i++ {
		outStr = strings.Join([]string{outStr, vec.At(i)}, "\n")
	}

	return strings.Join([]string{outStr, "}"}, "\n")
}
Example #2
0
// List all commands, one by each line, sorted alphabetically
func gtplist_commands(obj *GTPObject) *GTPCommand {
	signature := []int{}
	f := func(object *GTPObject, params []interface{}) (result string, quit bool, err Error) {
		result = ""
		var cmdVector vector.StringVector
		for cmdName, _ := range obj.commands {
			cmdVector.Push(cmdName)
		}
		sort.SortStrings(sort.StringArray(cmdVector))
		result += cmdVector[0]
		for i := 1; i < cmdVector.Len(); i++ {
			result += "\n" + cmdVector.At(i)
		}
		return result, false, nil
	}
	return &GTPCommand{Signature: signature,
		Func: f,
	}
}
Example #3
0
func (exchange *Exchange) handleTick(t int64) {
	removeTheseUnreadyQueues := new(vector.StringVector)
	for toAddress, messageQueue := range exchange.messageQueues {
		for i := 0; i < messageQueue.Len(); i++ {
			msg := messageQueue.At(i).(Message)
			if msg.timeout < t {
				exchange.logf(LogLevelDebug, "> %v %v %v %v", len(msg.Body), msg.TimeoutSeconds, msg.ToAddress, msg.ReplyAddress)
				exchange.logf(LogLevelDebug, "  send timeout")
				messageQueue.Delete(i)
				i--
			}
		}
		if messageQueue.Len() == 0 {
			removeTheseUnreadyQueues.Push(toAddress)
		}
	}
	for i := 0; i < removeTheseUnreadyQueues.Len(); i++ {
		exchange.messageQueues[removeTheseUnreadyQueues.At(i)] = nil, false
	}

	removeTheseReadyStates := new(vector.StringVector)
	for onAddress, readyStateQueue := range exchange.readyStateQueues {
		for i := 0; i < readyStateQueue.Len(); i++ {
			readyState := readyStateQueue.At(i).(*readyState)
			if readyState.timeout < t {
				exchange.logf(LogLevelDebug, "* ready timeout %v", onAddress)
				if !readyState.timeoutReceived {
					readyState.messageChan <- Message{"", "", 0, "", 0}
					readyState.timeoutReceived = true
				}
				readyStateQueue.Delete(i)
				i--
			}
		}
		if readyStateQueue.Len() == 0 {
			removeTheseReadyStates.Push(onAddress)
		}
	}
	for i := 0; i < removeTheseReadyStates.Len(); i++ {
		exchange.readyStateQueues[removeTheseReadyStates.At(i)] = nil, false
	}
}
Example #4
0
// As gtpkomoku_sourcen, except that it makes replaces the board with a copy of it in each move. This is used to debug
// Board.Copy()
func gtpkomoku_sourceforkn(obj *GTPObject) *GTPCommand {
	signature := []int{GTPString, GTPInt}
	f := func(object *GTPObject, params []interface{}) (result string, quit bool, err Error) {
		filename, _ := params[0].(string)
		n := int(params[1].(uint))
		file, er := os.Open(filename, os.O_RDONLY, 0)
		if er != nil {
			return fmt.Sprintf("error: '%s'", err), false, NewIOError(er)
		}
		input := bufio.NewReader(file)
		var lines vector.StringVector
		line, er := input.ReadString('\n')
		lines.Push(line)
		for er != os.EOF {
			lines.Push(line)
			line, er = input.ReadString('\n')
		}
		length := lines.Len()
		line = ""
		for i := 0; i < length; i++ {
			line = lines.At(i)
			if length-i <= n {
				line = "#" + line
			}
			fmt.Printf(line)
			obj.ai.environment.Game.Board = obj.ai.environment.Game.Board.Copy()
			lineResult, lineQuit, _ := obj.ExecuteCommand(line)
			fmt.Printf(lineResult)
			if lineQuit {
				return "", true, nil
			}
		}
		fmt.Println("\nend komoku-sourceforkn")
		return "", false, nil
	}
	return &GTPCommand{Signature: signature,
		Func: f,
	}
}
Example #5
0
func ExecutionModelSpec(c nanospec.Context) {

	c.Specify("Specs with children, but without siblings, are executed fully on one run", func() {

		c.Specify("Case: no children", func() {
			runSpecWithContext(DummySpecWithNoChildren, newInitialContext())
			c.Expect(testSpy).Equals("root")
		})
		c.Specify("Case: one child", func() {
			runSpecWithContext(DummySpecWithOneChild, newInitialContext())
			c.Expect(testSpy).Equals("root,a")
		})
		c.Specify("Case: nested children", func() {
			runSpecWithContext(DummySpecWithNestedChildren, newInitialContext())
			c.Expect(testSpy).Equals("root,a,aa")
		})
	})

	c.Specify("Specs with siblings are executed only one sibling at a time", func() {

		c.Specify("Case: on initial run, the 1st child is executed", func() {
			runSpecWithContext(DummySpecWithTwoChildren, newInitialContext())
			c.Expect(testSpy).Equals("root,a")
		})
		c.Specify("Case: explicitly execute the 1st child", func() {
			runSpecWithContext(DummySpecWithTwoChildren, newExplicitContext([]int{0}))
			c.Expect(testSpy).Equals("root,a")
		})
		c.Specify("Case: explicitly execute the 2nd child", func() {
			runSpecWithContext(DummySpecWithTwoChildren, newExplicitContext([]int{1}))
			c.Expect(testSpy).Equals("root,b")
		})
	})

	c.Specify("Specs with nested siblings: eventually all siblings are executed, one at a time, in isolation", func() {
		r := NewRunner()
		r.AddSpec(DummySpecWithMultipleNestedChildren)

		// Execute manually instead of calling Run(), in order to avoid running
		// the specs multi-threadedly, which would mess up the test spy.
		runs := new(vector.StringVector)
		for r.hasScheduledTasks() {
			resetTestSpy()
			r.executeNextScheduledTask()
			runs.Push(testSpy)
		}
		sort.Sort(runs)

		c.Expect(runs.Len()).Equals(5)
		c.Expect(runs.At(0)).Equals("root,a,aa")
		c.Expect(runs.At(1)).Equals("root,a,ab")
		c.Expect(runs.At(2)).Equals("root,b,ba")
		c.Expect(runs.At(3)).Equals("root,b,bb")
		c.Expect(runs.At(4)).Equals("root,b,bc")
	})
}
Example #6
0
func TestMultiFindValue(t *testing.T) {
	trie := NewTrie()

	// these are part of the matches for the word 'hyphenation'
	trie.AddPatternString(`hy3ph`)
	trie.AddPatternString(`he2n`)
	trie.AddPatternString(`hena4`)
	trie.AddPatternString(`hen5at`)

	v1 := &vector.IntVector{0, 3, 0, 0}
	v2 := &vector.IntVector{0, 2, 0}
	v3 := &vector.IntVector{0, 0, 0, 4}
	v4 := &vector.IntVector{0, 0, 5, 0, 0}

	expectStr := new(vector.StringVector)
	expectVal := new(vector.Vector) // contains elements of type *vector.IntVector

	expectStr.Push(`hyph`)
	expectVal.Push(v1)
	found, values := trie.AllSubstringsAndValues(`hyphenation`)
	if found.Len() != expectStr.Len() {
		t.Errorf("expected %v but found %v", *expectStr, *found)
	}
	if values.Len() != expectVal.Len() {
		t.Errorf("Length mismatch: expected %v but found %v", *expectVal, *values)
	}
	for i := 0; i < found.Len(); i++ {
		if found.At(i) != expectStr.At(i) {
			t.Errorf("Strings content mismatch: expected %v but found %v", *expectStr, *found)
			break
		}
	}
	for i := 0; i < values.Len(); i++ {
		ev := expectVal.At(i).(*vector.IntVector)
		fv := values.At(i).(*vector.IntVector)
		if ev.Len() != fv.Len() {
			t.Errorf("Value length mismatch: expected %v but found %v", *ev, *fv)
			break
		}
		for i := 0; i < ev.Len(); i++ {
			if ev.At(i) != fv.At(i) {
				t.Errorf("Value mismatch: expected %v but found %v", *ev, *fv)
				break
			}
		}
	}

	expectStr.Cut(0, expectStr.Len())
	expectVal.Cut(0, expectVal.Len())

	expectStr.AppendVector(&vector.StringVector{`hen`, `hena`, `henat`})
	expectVal.Push(v2)
	expectVal.Push(v3)
	expectVal.Push(v4)
	found, values = trie.AllSubstringsAndValues(`henation`)
	if found.Len() != expectStr.Len() {
		t.Errorf("expected %v but found %v", *expectStr, *found)
	}
	if values.Len() != expectVal.Len() {
		t.Errorf("Length mismatch: expected %v but found %v", *expectVal, *values)
	}
	for i := 0; i < found.Len(); i++ {
		if found.At(i) != expectStr.At(i) {
			t.Errorf("Strings content mismatch: expected %v but found %v", *expectStr, *found)
			break
		}
	}
	for i := 0; i < values.Len(); i++ {
		ev := expectVal.At(i).(*vector.IntVector)
		fv := values.At(i).(*vector.IntVector)
		if ev.Len() != fv.Len() {
			t.Errorf("Value length mismatch: expected %v but found %v", *ev, *fv)
			break
		}
		for i := 0; i < ev.Len(); i++ {
			if ev.At(i) != fv.At(i) {
				t.Errorf("Value mismatch: expected %v but found %v", *ev, *fv)
				break
			}
		}
	}
}