|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:random_access_source/random_access_source.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +SyncBytesRASource _bytesSource() { |
| 7 | + return SyncBytesRASource(Uint8List.fromList([1, 2, 3, 4, 5])); |
| 8 | +} |
| 9 | + |
| 10 | +void main() { |
| 11 | + test('Length', () async { |
| 12 | + final src = _bytesSource(); |
| 13 | + expect(src.length(), 5); |
| 14 | + }); |
| 15 | + |
| 16 | + test('ReadByte', () async { |
| 17 | + final src = _bytesSource(); |
| 18 | + expect(src.readByte(), 1); |
| 19 | + expect(src.position(), 1); |
| 20 | + |
| 21 | + expect(src.readByte(), 2); |
| 22 | + expect(src.position(), 2); |
| 23 | + |
| 24 | + expect(src.readByte(), 3); |
| 25 | + expect(src.position(), 3); |
| 26 | + |
| 27 | + expect(src.readByte(), 4); |
| 28 | + expect(src.position(), 4); |
| 29 | + |
| 30 | + expect(src.readByte(), 5); |
| 31 | + expect(src.position(), 5); |
| 32 | + |
| 33 | + expect(src.readByte(), -1); |
| 34 | + expect(src.position(), 5); |
| 35 | + }); |
| 36 | + |
| 37 | + test('Read', () async { |
| 38 | + final src = _bytesSource(); |
| 39 | + expect(src.read(2), Uint8List.fromList([1, 2])); |
| 40 | + expect(src.position(), 2); |
| 41 | + |
| 42 | + expect(src.read(2), Uint8List.fromList([3, 4])); |
| 43 | + expect(src.position(), 4); |
| 44 | + |
| 45 | + expect(src.read(2), Uint8List.fromList([5])); |
| 46 | + expect(src.position(), 5); |
| 47 | + |
| 48 | + expect(src.read(2), Uint8List(0)); |
| 49 | + expect(src.position(), 5); |
| 50 | + }); |
| 51 | + |
| 52 | + test('Position', () async { |
| 53 | + final src = _bytesSource(); |
| 54 | + expect(src.position(), 0); |
| 55 | + src.setPosition(2); |
| 56 | + expect(src.position(), 2); |
| 57 | + expect(src.readByte(), 3); |
| 58 | + }); |
| 59 | + |
| 60 | + test('ReadToEnd', () async { |
| 61 | + final src = _bytesSource(); |
| 62 | + expect(src.readToEnd(), Uint8List.fromList([1, 2, 3, 4, 5])); |
| 63 | + expect(src.position(), 5); |
| 64 | + }); |
| 65 | + |
| 66 | + test('ReadToEnd (halfway)', () async { |
| 67 | + final src = _bytesSource(); |
| 68 | + src.setPosition(2); |
| 69 | + expect(src.readToEnd(), Uint8List.fromList([3, 4, 5])); |
| 70 | + expect(src.position(), 5); |
| 71 | + }); |
| 72 | +} |
0 commit comments