OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.vlq_test; |
| 6 |
| 7 import 'dart:math'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 import 'package:source_maps/src/vlq.dart'; |
| 10 |
| 11 main() { |
| 12 test('encode and decode - simple values', () { |
| 13 expect(encodeVlq(1).join(''), 'C'); |
| 14 expect(encodeVlq(2).join(''), 'E'); |
| 15 expect(encodeVlq(3).join(''), 'G'); |
| 16 expect(encodeVlq(100).join(''), 'oG'); |
| 17 expect(decodeVlq('C'.split('').iterator), 1); |
| 18 expect(decodeVlq('E'.split('').iterator), 2); |
| 19 expect(decodeVlq('G'.split('').iterator), 3); |
| 20 expect(decodeVlq('oG'.split('').iterator), 100); |
| 21 }); |
| 22 |
| 23 test('encode and decode', () { |
| 24 for (int i = -10000; i < 10000; i++) { |
| 25 _checkEncodeDecode(i); |
| 26 } |
| 27 }); |
| 28 |
| 29 test('only 32-bit ints allowed', () { |
| 30 var max_int = pow(2, 31) - 1; |
| 31 var min_int = -pow(2, 31); |
| 32 _checkEncodeDecode(max_int - 1); |
| 33 _checkEncodeDecode(min_int + 1); |
| 34 _checkEncodeDecode(max_int); |
| 35 _checkEncodeDecode(min_int); |
| 36 |
| 37 expect(encodeVlq(min_int).join(''), 'hgggggE'); |
| 38 expect(decodeVlq('hgggggE'.split('').iterator), min_int); |
| 39 |
| 40 expect(() => encodeVlq(max_int + 1), throws); |
| 41 expect(() => encodeVlq(max_int + 2), throws); |
| 42 expect(() => encodeVlq(min_int - 1), throws); |
| 43 expect(() => encodeVlq(min_int - 2), throws); |
| 44 |
| 45 |
| 46 // if we allowed more than 32 bits, these would be the expected encodings |
| 47 // for the large numbers above. |
| 48 expect(() => decodeVlq('ggggggE'.split('').iterator), throws); |
| 49 expect(() => decodeVlq('igggggE'.split('').iterator), throws); |
| 50 expect(() => decodeVlq('jgggggE'.split('').iterator), throws); |
| 51 expect(() => decodeVlq('lgggggE'.split('').iterator), throws); |
| 52 }); |
| 53 } |
| 54 |
| 55 _checkEncodeDecode(int value) { |
| 56 var encoded = encodeVlq(value); |
| 57 expect(decodeVlq(encoded.iterator), value); |
| 58 expect(decodeVlq(encoded.join('').split('').iterator), value); |
| 59 } |
OLD | NEW |