serialization_test.dart 4.12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
@TestOn('!chrome') // web does not support certain 64bit behavior

7 8
import 'dart:typed_data';

9
import 'package:flutter/foundation.dart';
10
import '../flutter_test_alternative.dart';
11 12 13 14

void main() {
  group('Write and read buffer round-trip', () {
    test('of single byte', () {
15
      final WriteBuffer write = WriteBuffer();
16 17 18
      write.putUint8(201);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(1));
19
      final ReadBuffer read = ReadBuffer(written);
20 21 22
      expect(read.getUint8(), equals(201));
    });
    test('of 32-bit integer', () {
23
      final WriteBuffer write = WriteBuffer();
24 25 26
      write.putInt32(-9);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(4));
27
      final ReadBuffer read = ReadBuffer(written);
28 29
      expect(read.getInt32(), equals(-9));
    });
30 31 32 33 34 35 36 37
    test('of 32-bit integer in big endian', () {
      final WriteBuffer write = WriteBuffer();
      write.putInt32(-9, endian: Endian.big);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(4));
      final ReadBuffer read = ReadBuffer(written);
      expect(read.getInt32(endian: Endian.big), equals(-9));
    });
38
    test('of 64-bit integer', () {
39
      final WriteBuffer write = WriteBuffer();
40 41 42
      write.putInt64(-9000000000000);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(8));
43
      final ReadBuffer read = ReadBuffer(written);
44 45
      expect(read.getInt64(), equals(-9000000000000));
    });
46 47 48 49 50 51 52 53
    test('of 64-bit integer in big endian', () {
      final WriteBuffer write = WriteBuffer();
      write.putInt64(-9000000000000, endian: Endian.big);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(8));
      final ReadBuffer read = ReadBuffer(written);
      expect(read.getInt64(endian: Endian.big), equals(-9000000000000));
    });
54
    test('of double', () {
55
      final WriteBuffer write = WriteBuffer();
56 57 58
      write.putFloat64(3.14);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(8));
59
      final ReadBuffer read = ReadBuffer(written);
60 61
      expect(read.getFloat64(), equals(3.14));
    });
62 63 64 65 66 67 68 69
    test('of double in big endian', () {
      final WriteBuffer write = WriteBuffer();
      write.putFloat64(3.14, endian: Endian.big);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(8));
      final ReadBuffer read = ReadBuffer(written);
      expect(read.getFloat64(endian: Endian.big), equals(3.14));
    });
70
    test('of 32-bit int list when unaligned', () {
71 72
      final Int32List integers = Int32List.fromList(<int>[-99, 2, 99]);
      final WriteBuffer write = WriteBuffer();
73 74 75 76
      write.putUint8(9);
      write.putInt32List(integers);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(16));
77
      final ReadBuffer read = ReadBuffer(written);
78 79 80 81
      read.getUint8();
      expect(read.getInt32List(3), equals(integers));
    });
    test('of 64-bit int list when unaligned', () {
82 83
      final Int64List integers = Int64List.fromList(<int>[-99, 2, 99]);
      final WriteBuffer write = WriteBuffer();
84 85 86 87
      write.putUint8(9);
      write.putInt64List(integers);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(32));
88
      final ReadBuffer read = ReadBuffer(written);
89 90 91 92
      read.getUint8();
      expect(read.getInt64List(3), equals(integers));
    });
    test('of double list when unaligned', () {
93 94
      final Float64List doubles = Float64List.fromList(<double>[3.14, double.nan]);
      final WriteBuffer write = WriteBuffer();
95 96 97 98
      write.putUint8(9);
      write.putFloat64List(doubles);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(24));
99
      final ReadBuffer read = ReadBuffer(written);
100 101 102 103 104 105
      read.getUint8();
      final Float64List readDoubles = read.getFloat64List(2);
      expect(readDoubles[0], equals(3.14));
      expect(readDoubles[1], isNaN);
    });
  });
106
}