| 1 | module vlq |
| 2 | |
| 3 | import io |
| 4 | |
| 5 | struct TestReader { |
| 6 | pub: |
| 7 | bytes []u8 |
| 8 | mut: |
| 9 | i int |
| 10 | } |
| 11 | |
| 12 | struct TestData { |
| 13 | decode_val string |
| 14 | expected i64 |
| 15 | } |
| 16 | |
| 17 | type TestDataList = []TestData |
| 18 | |
| 19 | fn test_decode_a() { |
| 20 | decode_values := [ |
| 21 | TestData{'A', 0}, |
| 22 | TestData{'C', 1}, |
| 23 | TestData{'D', -1}, |
| 24 | TestData{'2H', 123}, |
| 25 | TestData{'qxmvrH', 123456789}, |
| 26 | TestData{'+/////B', 1073741823}, // 2^30-1 |
| 27 | // TestData{'hgggggggggggI', 9_223_372_036_854_775_808} // 2^63 |
| 28 | ] |
| 29 | |
| 30 | for _, test_data in decode_values { |
| 31 | mut input := make_test_reader(test_data.decode_val) |
| 32 | |
| 33 | res := decode(mut &input)! |
| 34 | assert res == test_data.expected |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn (mut b TestReader) read(mut buf []u8) !int { |
| 39 | if !(b.i < b.bytes.len) { |
| 40 | return io.Eof{} |
| 41 | } |
| 42 | n := copy(mut buf, b.bytes[b.i..]) |
| 43 | b.i += n |
| 44 | return n |
| 45 | } |
| 46 | |
| 47 | fn make_test_reader(data string) io.Reader { |
| 48 | buf := &TestReader{ |
| 49 | bytes: data.bytes() |
| 50 | } |
| 51 | return io.new_buffered_reader(reader: buf) |
| 52 | } |
| 53 | |