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