コード例 #1
0
ファイル: stack_test.go プロジェクト: traplol/learning-go
func TestStackPush(t *testing.T) {
	stack := stack.NewStack()
	stack.Push(42)
	if stack.Count() != 1 {
		t.Error("expected stack count to be: 1")
	}
	stack.Push(2)
	if stack.Count() != 2 {
		t.Error("expected stack count to be: 2")
	}
}
コード例 #2
0
ファイル: stack_test.go プロジェクト: traplol/learning-go
func TestStackPop(t *testing.T) {
	stack := stack.NewStack()
	stack.Push(42)
	stack.Push(2)

	var v int
	var empty bool

	v, empty = stack.Pop()
	if e := 2; v != e {
		t.Errorf("expected stack to pop the value: %d, got %d", e, v)
	}
	if empty != false {
		t.Error("expected stack to not be empty.")
	}
	if e := 1; stack.Count() != e {
		t.Errorf("expected stack count to be: %d, got %d", e, v)
	}

	v, empty = stack.Pop()
	if e := 42; v != e {
		t.Errorf("expected stack to pop the value: %d, got %d", e, v)
	}
	if empty != true {
		t.Error("expected stack to be empty.")
	}
	if e := 0; stack.Count() != e {
		t.Errorf("expected stack count to be: %d, got %d", e, v)
	}

	stack.Push(100)
	v, empty = stack.Pop()
	if e := 100; v != e {
		t.Errorf("expected stack to pop the value: %d, got %d", e, v)
	}
	if empty != true {
		t.Error("expected stack to be empty.")
	}
	if e := 0; stack.Count() != e {
		t.Errorf("expected stack count to be: %d, got %d", e, v)
	}

	v, empty = stack.Pop()
	if e := 0; v != e {
		t.Errorf("expected stack to pop the value: %d, got %d", e, v)
	}
	if empty != true {
		t.Error("expected stack to be empty.")
	}
	if e := 0; stack.Count() != e {
		t.Errorf("expected stack count to be: %d, got %d", e, v)
	}
}
コード例 #3
0
ファイル: stack_test.go プロジェクト: traplol/learning-go
func TestManyPushes(t *testing.T) {
	count := 10000
	stack := stack.NewStack()
	for i := 0; i < count; i++ {
		stack.Push(i + 1)
	}
	if a := stack.Count(); a != count {
		t.Errorf("Expected stack count to be %d, got %d", count, a)
	}
	for e := count; e > 0; e-- {
		if a, _ := stack.Pop(); a != e {
			t.Errorf("Expected stack to pop %d, got %d", e, a)
		}
	}
}
コード例 #4
0
ファイル: interp.go プロジェクト: traplol/learning-go
func parse(code []byte) {
	for _, c := range code {
		switch c {
		default:
			continue
		case '>':
			instructions = append(instructions, &Instruction{Op: Right, JumpTo: 0})
		case '<':
			instructions = append(instructions, &Instruction{Op: Left, JumpTo: 0})
		case '+':
			instructions = append(instructions, &Instruction{Op: Add, JumpTo: 0})
		case '-':
			instructions = append(instructions, &Instruction{Op: Sub, JumpTo: 0})
		case '.':
			instructions = append(instructions, &Instruction{Op: Out, JumpTo: 0})
		case ',':
			instructions = append(instructions, &Instruction{Op: In, JumpTo: 0})
		case '[':
			instructions = append(instructions, &Instruction{Op: LoopBeg, JumpTo: 0})
		case ']':
			instructions = append(instructions, &Instruction{Op: LoopEnd, JumpTo: 0})
		}
	}
	// Calculate jumps.
	s := stack.NewStack()
	for i := 0; i < len(instructions); i++ {
		op := instructions[i]
		if op.Op != LoopBeg {
			continue
		}
		for j := i; j < len(instructions); j++ {
			op2 := instructions[j]
			if op2.Op == LoopBeg {
				s.Push(1)
			} else if op2.Op == LoopEnd {
				_, empty := s.Pop()
				if empty {
					op.JumpTo = j + 1
					op2.JumpTo = i
					break
				}
			}
		}
	}
}