|
| 1 | +package conversion |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +var binaryTestCases = map[string]int{ |
| 6 | + "0": 0, "1": 1, "10": 2, "11": 3, "100": 4, |
| 7 | + "101": 5, "110": 6, "111": 7, "1000": 8, "1001": 9, |
| 8 | + "1010": 10, "1011": 11, "1100": 12, "1101": 13, "1110": 14, |
| 9 | + "1111": 15, "10000": 16, "10001": 17, "10010": 18, "10011": 19, |
| 10 | + "10100": 20, "10101": 21, "10110": 22, "10111": 23, "11000": 24, |
| 11 | + "11001": 25, "11010": 26, "11011": 27, "11100": 28, "11101": 29, |
| 12 | + "11110": 30, "11111": 31, "100000": 32, "100001": 33, "100010": 34, |
| 13 | + "100011": 35, "100100": 36, "100101": 37, "100110": 38, "100111": 39, |
| 14 | + "101000": 40, "101001": 41, "101010": 42, "101011": 43, "101100": 44, |
| 15 | + "101101": 45, "101110": 46, "101111": 47, "110000": 48, "110001": 49, |
| 16 | + "110010": 50, "110011": 51, "110100": 52, "110101": 53, "110110": 54, |
| 17 | + "110111": 55, "111000": 56, "111001": 57, "111010": 58, "111011": 59, |
| 18 | + "111100": 60, "111101": 61, "111110": 62, "111111": 63, "1000000": 64, |
| 19 | + "1000001": 65, "1000010": 66, "1000011": 67, "1000100": 68, "1000101": 69, |
| 20 | + "1000110": 70, "1000111": 71, "1001000": 72, "1001001": 73, "1001010": 74, |
| 21 | + "1001011": 75, "1001100": 76, "1001101": 77, "1001110": 78, "1001111": 79, |
| 22 | + "1010000": 80, "1010001": 81, "1010010": 82, "1010011": 83, "1010100": 84, |
| 23 | + "1010101": 85, "1010110": 86, "1010111": 87, "1011000": 88, "1011001": 89, |
| 24 | + "1011010": 90, "1011011": 91, "1011100": 92, "1011101": 93, "1011110": 94, |
| 25 | + "1011111": 95, "1100000": 96, "1100001": 97, "1100010": 98, "1100011": 99, |
| 26 | + "1100100": 100, |
| 27 | +} |
| 28 | + |
| 29 | +func TestBinaryToDecimal(t *testing.T) { |
| 30 | + for input, expected := range binaryTestCases { |
| 31 | + out, err := BinaryToDecimal(input) |
| 32 | + if err != nil { |
| 33 | + t.Errorf("BinaryToDecimal(%s) returned an error %s", input, err.Error()) |
| 34 | + } |
| 35 | + if out != expected { |
| 36 | + t.Errorf("BinaryToDecimal(%s) = %d; want %d", input, out, expected) |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func BenchmarkBinaryToDecimal(b *testing.B) { |
| 42 | + b.ReportAllocs() |
| 43 | + for i := 0; i < b.N; i++ { |
| 44 | + _, _ = BinaryToDecimal("1100100") |
| 45 | + } |
| 46 | +} |
0 commit comments