serialization_test.dart 3.06 KB
Newer Older
1 2 3 4
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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 30
      expect(read.getInt32(), equals(-9));
    });
    test('of 64-bit integer', () {
31
      final WriteBuffer write = WriteBuffer();
32 33 34
      write.putInt64(-9000000000000);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(8));
35
      final ReadBuffer read = ReadBuffer(written);
36 37 38
      expect(read.getInt64(), equals(-9000000000000));
    });
    test('of double', () {
39
      final WriteBuffer write = WriteBuffer();
40 41 42
      write.putFloat64(3.14);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(8));
43
      final ReadBuffer read = ReadBuffer(written);
44 45 46
      expect(read.getFloat64(), equals(3.14));
    });
    test('of 32-bit int list when unaligned', () {
47 48
      final Int32List integers = Int32List.fromList(<int>[-99, 2, 99]);
      final WriteBuffer write = WriteBuffer();
49 50 51 52
      write.putUint8(9);
      write.putInt32List(integers);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(16));
53
      final ReadBuffer read = ReadBuffer(written);
54 55 56 57
      read.getUint8();
      expect(read.getInt32List(3), equals(integers));
    });
    test('of 64-bit int list when unaligned', () {
58 59
      final Int64List integers = Int64List.fromList(<int>[-99, 2, 99]);
      final WriteBuffer write = WriteBuffer();
60 61 62 63
      write.putUint8(9);
      write.putInt64List(integers);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(32));
64
      final ReadBuffer read = ReadBuffer(written);
65 66 67 68
      read.getUint8();
      expect(read.getInt64List(3), equals(integers));
    });
    test('of double list when unaligned', () {
69 70
      final Float64List doubles = Float64List.fromList(<double>[3.14, double.nan]);
      final WriteBuffer write = WriteBuffer();
71 72 73 74
      write.putUint8(9);
      write.putFloat64List(doubles);
      final ByteData written = write.done();
      expect(written.lengthInBytes, equals(24));
75
      final ReadBuffer read = ReadBuffer(written);
76 77 78 79 80 81
      read.getUint8();
      final Float64List readDoubles = read.getFloat64List(2);
      expect(readDoubles[0], equals(3.14));
      expect(readDoubles[1], isNaN);
    });
  });
82
}