예제 #1
0
func TestRomanNumeralsToUint(t *testing.T) {
	shouldAssert := []struct {
		input string
		msg   string
	}{
		{input: "", msg: "empty string is invalid"},
		{input: "JTXIV", msg: "unrecognised sequence: JTXIV"},
		{input: "XIJI", msg: "unrecognised sequence: JI"},
		{input: "DIXX", msg: "sequence \"X\" followed smaller sequence \"IX\""},
	}
	for _, test := range shouldAssert {
		func() {
			defer assert.Panics(t, "romanNumeralsToUint(\""+test.input+"\")",
				test.msg)
			_ = romanNumeralsToUint(test.input)
		}()
	}

	valid := []struct {
		input  string
		result uint
	}{
		{input: "III", result: 3},
		{input: "V", result: 5},
		{input: "X", result: 10},
		{input: "XIX", result: 19},
		{input: "LV", result: 55},
		{input: "DCLXVI", result: 666},
		{input: "MCMXCVII", result: 1997},
	}
	for _, test := range valid {
		result := romanNumeralsToUint(test.input)
		assert.Equal(t, "romanNumeralsToUint(\""+test.input+"\")", test.result, result)
	}
}
예제 #2
0
func TestOpenOrDie(t *testing.T) {
	// Should work.
	_ = openOrDie(os.DevNull)
	path := "/qwertyuiop/asdfghjkl"
	defer assert.Panics(t, "openOrDie() should panic", path)
	_ = openOrDie(path)
}
예제 #3
0
func TestReadLinesFromFile(t *testing.T) {
	fh := bytes.NewBufferString("1\n2 3\n")
	expected := []string{"1", "2 3"}
	actual := readLinesFromFile(fh)
	assert.Equal(t, "readLinesFromFile bad result", expected, actual)

	fh2 := iotest.TimeoutReader(bytes.NewBufferString("1\n2 3\n"))
	defer assert.Panics(t, "readLinesFromFile() should panic on failure", "Reading lines failed")
	_ = readLinesFromFile(fh2)
}
예제 #4
0
func TestParseTriangle(t *testing.T) {
	fh := bytes.NewBufferString("1\n2 3\n")
	triangle := parseTriangle(fh)
	expected := [][]int{
		[]int{1},
		[]int{2, 3},
	}
	assert.Equal(t, "parseTriangle()", expected, triangle)

	defer assert.Panics(t, "parseTriangle() should panic", "invalid syntax")
	fh = bytes.NewBufferString("x\n")
	triangle = parseTriangle(fh)
}
예제 #5
0
func TestFactorialInt64(t *testing.T) {
	tests := []struct {
		n, e int64
	}{
		{1, 1},
		{4, 24},
		{7, 5040},
	}
	for _, test := range tests {
		assert.Equal(t, fmt.Sprintf("factorialInt64(%v)", test.n), test.e, factorialInt64(test.n))
	}

	defer assert.Panics(t, "factorialInt64 should have called panic", "n too big")
	_ = factorialInt64(25)
}
예제 #6
0
func TestReadIntsFromCSVFile(t *testing.T) {
	tests := []struct {
		csv, err string
		expected [][]int64
	}{
		{csv: "1,7,42", expected: [][]int64{{1, 7, 42}}},
		{csv: "1,7,42,qwerty", err: "strconv.ParseInt"},
		{csv: "1,7,42,qwe\"rty", err: "non-quoted-field"},
	}
	for _, test := range tests {
		func() {
			desc := fmt.Sprintf("readIntsFromCSVFile(%q)", test.csv)
			if test.err != "" {
				defer assert.Panics(t, desc, test.err)
			}
			actual := readIntsFromCSVFile(strings.NewReader(test.csv))
			// This will be skipped for the error cases.
			assert.Equal(t, desc, test.expected, actual)
		}()
	}
}
예제 #7
0
func TestMarkovMatrixInvarientCheck(t *testing.T) {
	// The code should work properly with a 1x1 array, so keep it simple.
	tests := []struct {
		err string
		rat *big.Rat
	}{
		{"", big.NewRat(1, 1)},
		{"is negative", big.NewRat(-1, 1)},
		{"sum of row", big.NewRat(2, 1)},
	}
	for _, test := range tests {
		func() {
			matrix := [][]*big.Rat{{test.rat}}
			if test.err == "" {
				MarkovMatrixInvarientCheck(matrix, -1)
			} else {
				defer assert.Panics(t, "MarkovMatrixInvarientCheck should have called panic()", test.err)
				MarkovMatrixInvarientCheck(matrix, -1)
			}
		}()
	}
}
예제 #8
0
func TestUintToRomanNumerals(t *testing.T) {
	func() {
		defer assert.Panics(t, "uintToRomanNumerals(0)", "0 is invalid")
		_ = uintToRomanNumerals(0)
	}()

	valid := []struct {
		input  uint
		result string
	}{
		{input: 3, result: "III"},
		{input: 5, result: "V"},
		{input: 10, result: "X"},
		{input: 19, result: "XIX"},
		{input: 55, result: "LV"},
		{input: 666, result: "DCLXVI"},
		{input: 1997, result: "MCMXCVII"},
	}
	for _, test := range valid {
		result := uintToRomanNumerals(test.input)
		msg := "uintToRomanNumerals(" + strconv.FormatUint(uint64(test.input), 10) + ")"
		assert.Equal(t, msg, test.result, result)
	}
}
예제 #9
0
func TestFirstBiggerElement(t *testing.T) {
	l := []int{1, 3, 5, 7, 11}
	assert.Equal(t, "firstBiggerElement works", 5, firstBiggerElement(4, l))
	defer assert.Panics(t, "firstBiggerElement should have called panic", "bigger than")
	_ = firstBiggerElement(13, l)
}