Ejemplo n.º 1
0
func TestSumLinkedListsReverse(t *testing.T) {
	for _, test := range []TestCase{
		{"[1,2,3,4]", "[5,6,7,8]", "[6,9,1,2]"},
		{"[1,2,3,4,5]", "[5,6,7,8]", "[1,8,0,2,3]"},
		{"[1,2,3,4]", "[5,6,7,8,6]", "[5,8,0,2,0]"},
		{"[9,9,9,9]", "[9,9,9,9]", "[1,9,9,9,8]"},
	} {
		l1 := linkedlist.FromJSON(test.listone)
		l2 := linkedlist.FromJSON(test.listtwo)
		sum := SumLinkedListsReverse(l1, l2)
		if sum.JSON() != test.expected {
			t.Errorf("Addition of %s and %s failed\n\t got %s expected%s", l1.JSON(), l2.JSON(), sum.JSON(), test.expected)
		}
	}
}
Ejemplo n.º 2
0
func TestDeleteNode(t *testing.T) {
	for _, test := range tests {
		l := linkedlist.FromJSON(test.Input)
		n := l.NodeAtPos(test.DelPos)
		DeleteNode(n)
		output := l.JSON()
		if output != test.Expected {
			t.Errorf("DeleteNode(*n) failed deleting node at %d of LinkedList length %d\n\tgot %s, expected %s", test.DelPos, l.Length, output, test.Expected)
		}
	}
}
Ejemplo n.º 3
0
func TestPartitionLinkedList(t *testing.T) {
	for _, test := range tests {
		l := linkedlist.FromJSON(test.Input)
		val := l.NodeAtPos(test.PartitionValDest).Value
		PartitionLinkedList(l, val)
		output := l.JSON()
		if output != test.Expected {
			t.Errorf("Partition(*LinkedList) failed partitioning around value %d\n\tgot %s, expected %s", val, output, test.Expected)
		}
	}
}
Ejemplo n.º 4
0
func tableTests(t *testing.T, testFunc q02Function) {
	funcName := nameOf(testFunc)

	for _, test := range tests {
		l := linkedlist.FromJSON(test.Input)
		KthNode := testFunc(l, test.K)
		if test.KthValue != KthNode.Value {
			t.Errorf("%s(%s) failed\n\tgot node with value %d, expected %d", funcName)
		}
	}
}
Ejemplo n.º 5
0
func TestIsPalindrome(t *testing.T) {
	for _, test := range []TestCase{
		{"[1,2,3,2,1]", true},
		{"[1,2,3,2,1,2]", false},
		{"[1,2,2,1]", true},
		{"[1,2,3,4,5,4,3,2,1]", true},
		{"[1,2,3,4,5,5,4,3,2,1]", true},
		{"[1,2,3,4,5,5,6,4,3,2,1]", false},
	} {
		l := linkedlist.FromJSON(test.input)
		result := IsPalindrome(l)
		if result != test.expected {
			t.Errorf("IsPalindrome(%s) failed, expected %b got %b", test.input, test.expected, result)
		}
	}
}
Ejemplo n.º 6
0
func runTestSuite(t *testing.T, testFunc func(l *linkedlist.LinkedList)) {
	funcName := nameOf(testFunc)

	for _, testcase := range tests {
		llist := linkedlist.FromJSON(testcase.Input)
		testFunc(llist)
		output := llist.JSON()
		if output != testcase.Expected {
			t.Errorf("%s(*LinkedList) failed\n\tinput:%s\n\texpected:%s\n\tgot %s", funcName, testcase.Input, testcase.Expected, output)
		}

		if llist.Length != testcase.ExpectedLength {
			t.Errorf("%s(*LinkedList) failed\n\tinput:%s\n\texpected length:%d\n\tgot length: %d", funcName, testcase.Input, testcase.ExpectedLength, llist.Length)
		}
	}
}
Ejemplo n.º 7
0
func runTestSuite(testFunc func(*linkedlist.LinkedList) *linkedlist.Node, t *testing.T) {
	funcName := nameOf(testFunc)

	for _, test := range []TestCase{
		{"[1,2,3,4]", 2, 3},
		{"[1,2,3,4,5,6]", 4, 5},
		{"[1,1]", 1, 1},
		{"[1]", 0, 1},
		{"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]", 10, 11},
	} {
		l := linkedlist.FromJSON(test.input)
		n := l.NodeAtPos(test.intersectionpos)
		l.Head.Next = n
		res := testFunc(l)
		if res.Value != test.expectedval {
			t.Errorf("%s(%s) failed finding loop with intersection node located at node %d, expected node val %d got node val %d",
				funcName, test.input, test.intersectionpos, test.expectedval, res.Value)
		}
	}
}