Пример #1
0
func TestUniPaths(t *testing.T) {
	is := testy.New(t)
	defer func() { t.Logf(is.Done()) }()

	// build from Reader
	b := bufio.NewReader(strings.NewReader(tinyG))
	g, err := ReadUniGraph(b)
	is.Equal(err, nil)
	is.Equal(g.V(), 13)
	is.Equal(g.E(), 13)
	is.Equal(g.Adjacent(0), []int{5, 1, 2, 6})

	// test DFS
	p := g.DFS(0)
	is.True(p.HasPathTo(4))
	is.False(p.HasPathTo(8))
	if path, ok := p.PathTo(3); ok {
		is.Equal(path, []int{0, 5, 4, 3})
	} else {
		is.Error("PathTo(3) failed")
	}

	// test BFS
	p = g.BFS(0)
	is.True(p.HasPathTo(4))
	is.False(p.HasPathTo(8))
	if path, ok := p.PathTo(3); ok {
		is.Equal(path, []int{0, 5, 3})
	} else {
		is.Error("PathTo(3) failed")
	}
}
Пример #2
0
func TestRandomOrdering(t *testing.T) {
	is := testy.New(t).Label("the order is preserved")
	defer func() { t.Logf(is.Done()) }()

	inOrder := inorder.NewInOrder(100 * time.Millisecond)

	var tasks []*DummyTask
	for i := 0; i < 5000; i++ {
		name := fmt.Sprintf("normal-%d", i)
		ms := rand.Int() % 50
		if i%500 == 0 {
			ms = 20000
			name = fmt.Sprintf("too-long-%d", i)
		}
		task := NewDummyTask(time.Duration(ms)*time.Millisecond, name)
		tasks = append(tasks, task)
		inOrder.Enqueue(task)
		if i%100 == 0 {
			time.Sleep(20 * time.Millisecond)
		}
	}

	for i := range tasks {
		result := <-inOrder.Done
		if result.Error == inorder.ErrTaskTimedOut {
			is.Equal(result.Task.(*DummyTask).ExpectedName, fmt.Sprintf("too-long-%d", i))
		} else if result.Error == nil {
			is.Equal(result.Task.(*DummyTask).Name, fmt.Sprintf("normal-%d", i))
		} else {
			is.Errorf("Task failed: %s", result.Error.Error())
		}
	}
}
Пример #3
0
func TestExample3(t *testing.T) {
	is := testy.New(t)
	defer func() { t.Logf(is.Done()) }()

	for i := 1; i <= 5; i++ {
		is.Label("Checking", i).True(i == 3) // Line 13
	}
}
Пример #4
0
func TestExample4(t *testing.T) {
	is := testy.New(t)
	defer func() { t.Logf(is.Done()) }()

	for i := -1; i <= 2; i++ {
		checkEvenPositive(is, i) // Line 13
	}
}
Пример #5
0
func TestLabelUplevel(t *testing.T) {
	mock := &testing.T{}
	test := testy.New(mock)

	checkEven(test, 3) // Line 70

	output := test.Output()
	if ok, _ := regexp.MatchString(`testy_test.go:70: Testing 3: Value is not even`, output[0]); !ok {
		t.Errorf("checkEven() had wrong error message: '%s'", output[0])
	}

}
Пример #6
0
func TestExample2(t *testing.T) {
	is := testy.New(t)
	defer func() { t.Logf(is.Done()) }()

	is.True(1+1 == 3)                          // Line 17
	is.False(2 == 2)                           // Line 18
	is.Equal(1, 2)                             // Line 19
	is.Equal(1.0, 1)                           // Line 20
	is.Equal("foo\tbar", "foo\tbaz")           // Line 21
	is.Equal(true, false)                      // Line 22
	is.Equal(&pair{1.0, 1.0}, &pair{1.1, 1.0}) // Line 23
	is.Unequal(42, 42)                         // Line 24
}
Пример #7
0
func TestHelpers(t *testing.T) {
	mock := &testing.T{}
	test := testy.New(mock)

	// not failures
	test.True(true)
	test.False(false)

	// failures on lines 24+; check later
	test.True(false)
	test.False(true)
	test.Equal(1, 2)
	test.Unequal("foo", "foo")
	test.Unequal(true, true)

	fc := test.FailCount()
	expect := 5
	if fc != expect {
		t.Errorf("Incorrect FailCount. Got %d, but expected %d", fc, expect)
	}

	output := test.Output()
	if ok, _ := regexp.MatchString("testy_test.go:24: Expression was not true", output[0]); !ok {
		t.Errorf("True() had wrong error message: '%s'", output[0])
	}
	if ok, _ := regexp.MatchString("testy_test.go:25: Expression was not false", output[1]); !ok {
		t.Errorf("False() had wrong error message: '%s'", output[1])
	}
	if ok, _ := regexp.MatchString("testy_test.go:26: Values were not equal", output[2]); !ok {
		t.Errorf("Equal() had wrong error message: '%s'", output[2])
	}
	if ok, _ := regexp.MatchString(`(?m)^\s+Got: 1 \(int\)`, output[2]); !ok {
		t.Errorf("Equal() had wrong 'got' message: '%s'", output[2])
	}
	if ok, _ := regexp.MatchString(`(?m)^\s+Wanted: 2 \(int\)`, output[2]); !ok {
		t.Errorf("Equal() had wrong 'got' message: '%s'", output[2])
	}
	if ok, _ := regexp.MatchString("testy_test.go:27: Values were not unequal", output[3]); !ok {
		t.Errorf("Unequal() had wrong error message: '%s'", output[3])
	}
	if ok, _ := regexp.MatchString(`(?m)^\s+Got: "foo"`, output[3]); !ok {
		t.Errorf("Unequal() had wrong 'got' message: '%s'", output[3])
	}
	if ok, _ := regexp.MatchString("testy_test.go:28: Values were not unequal", output[4]); !ok {
		t.Errorf("Unequal() had wrong error message: '%s'", output[4])
	}
	if ok, _ := regexp.MatchString(`(?m)^\s+Got: true`, output[4]); !ok {
		t.Errorf("Unequal() had wrong 'got' message: '%s'", output[4])
	}
}
Пример #8
0
func TestFail(t *testing.T) {
	mock := &testing.T{}
	test := testy.New(mock)

	if test.FailCount() != 0 {
		t.Errorf("FailCount didn't start at zero")
	}

	test.Fail()

	if !test.Failed() {
		t.Errorf("Fail() not recorded in facade")
	}

	if !mock.Failed() {
		t.Errorf("Fail() not recorded in test object")
	}
}
Пример #9
0
func TestErrorf(t *testing.T) {
	mock := &testing.T{}
	test := testy.New(mock)

	test.Errorf("%s %d", "three", 4)

	if !test.Failed() {
		t.Errorf("Errorf() not recorded in facade")
	}

	if !mock.Failed() {
		t.Errorf("Errorf() not recorded in test object")
	}

	output := test.Output()
	if ok, _ := regexp.MatchString(`testy_test.go:\d+: three 4`, output[0]); !ok {
		t.Errorf("Errorf() had wrong error message: '%s'", output[0])
	}
}
Пример #10
0
func TestUniGraph(t *testing.T) {
	is := testy.New(t)
	defer func() { t.Logf(is.Done()) }()

	// constructor
	g := NewUniGraph()

	is.Equal(g.V(), 0)
	is.Equal(g.E(), 0)

	// add edge
	g.AddEdge(1, 2)

	is.Equal(g.V(), 2)
	is.Equal(g.E(), 1)

	// add vertex
	g.AddVertex(0)
	is.Equal(g.V(), 3)
	is.Equal(g.E(), 1)

	// self loops shouldn't be added
	g.AddEdge(3, 3)
	is.Equal(g.V(), 4)
	is.Equal(g.E(), 1)

	// adjacency list
	g.AddEdge(3, 4)
	g.AddEdge(3, 5)
	g.AddEdge(3, 2)
	is.Equal(g.Adjacent(3), []int{4, 5, 2})

	// adjacency is read-only
	adj := g.Adjacent(3)
	adj[0] = 0
	is.Equal(g.Adjacent(3), []int{4, 5, 2})
}
Пример #11
0
func TestExample1(t *testing.T) {
	is := testy.New(t)
	defer func() { t.Logf(is.Done()) }() // Line 10
	is.Error("First failure")            // Line 11
	checkTrue(is, 1+1 == 3)              // Line 12
}