|
1 | 1 | import 'dart:ffi'; |
2 | 2 | import 'dart:typed_data'; |
3 | 3 |
|
4 | | -import 'package:test/test.dart'; |
5 | 4 | import 'package:flat_buffers/flat_buffers.dart' as fb_upstream; |
| 5 | +import 'package:objectbox/internal.dart'; |
6 | 6 | import 'package:objectbox/src/bindings/flatbuffers.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +import 'entity.dart'; |
| 10 | +import 'objectbox.g.dart'; |
| 11 | +import 'test_env.dart'; |
7 | 12 |
|
8 | 13 | Uint8List addFbData(dynamic fbb) { |
9 | 14 | fbb.startTable(); |
@@ -38,4 +43,87 @@ void main() { |
38 | 43 | fb1.clear(); |
39 | 44 | }); |
40 | 45 | }); |
| 46 | + |
| 47 | + final bytesSum = |
| 48 | + (ByteData data) => data.buffer.asInt8List().reduce((v, e) => v + e); |
| 49 | + |
| 50 | + test('allocator', () { |
| 51 | + final allocator = Allocator(); |
| 52 | + |
| 53 | + final buf1 = allocator.allocate(1024); |
| 54 | + allocator.clear(buf1, true); |
| 55 | + |
| 56 | + final buf2 = allocator.allocate(1024); |
| 57 | + allocator.clear(buf2, true); |
| 58 | + |
| 59 | + expect(bytesSum(buf1), isZero); |
| 60 | + expect(bytesSum(buf2), isZero); |
| 61 | + |
| 62 | + buf2.setInt8(42, 1); |
| 63 | + expect(bytesSum(buf1), isZero); |
| 64 | + expect(bytesSum(buf2), 1); |
| 65 | + |
| 66 | + allocator.clear(buf2, true); |
| 67 | + expect(bytesSum(buf2), isZero); |
| 68 | + |
| 69 | + allocator.deallocate(buf1); |
| 70 | + allocator.freeAll(); |
| 71 | + }); |
| 72 | + |
| 73 | + // Note: only checks content initialized by TestEntity.filled |
| 74 | + void checkSameEntities(TestEntity a, TestEntity b) { |
| 75 | + expect(a.tString, b.tString); |
| 76 | + expect(a.tBool, b.tBool); |
| 77 | + expect(a.tByte, b.tByte); |
| 78 | + expect(a.tChar, b.tChar); |
| 79 | + expect(a.tShort, b.tShort); |
| 80 | + expect(a.tInt, b.tInt); |
| 81 | + expect(a.tLong, b.tLong); |
| 82 | + expect(a.tFloat, b.tFloat); |
| 83 | + expect(a.tDouble, b.tDouble); |
| 84 | + expect(a.tStrings, b.tStrings); |
| 85 | + expect(a.tByteList, b.tByteList); |
| 86 | + expect(a.tInt8List, b.tInt8List); |
| 87 | + expect(a.tUint8List, b.tUint8List); |
| 88 | + } |
| 89 | + |
| 90 | + test('generated code', () { |
| 91 | + final env = TestEnv('fb'); |
| 92 | + |
| 93 | + final binding = getObjectBoxModel().bindings[TestEntity] |
| 94 | + as EntityDefinition<TestEntity>; |
| 95 | + |
| 96 | + final source = TestEntity.filled(); |
| 97 | + |
| 98 | + final fb1 = BuilderWithCBuffer(); |
| 99 | + binding.objectToFB(source, fb1.fbb); |
| 100 | + final fbData = fb1.bufPtr.cast<Uint8>().asTypedList(fb1.fbb.size); |
| 101 | + |
| 102 | + // must have the same content after reading back |
| 103 | + final target = binding.objectFromFB(env.store, fbData); |
| 104 | + |
| 105 | + // Note: we don't do check yet, because the default flatbuffers reader |
| 106 | + // reads lists lazily, on the first access and this would cause the next |
| 107 | + // [checkSameEntities()] after clearing the buffer to also pass. |
| 108 | + // checkSameEntities(target, source); |
| 109 | + |
| 110 | + // explicitly clear the allocated memory |
| 111 | + fbMemset(fb1.bufPtr.cast<Uint8>(), 0, fbData.lengthInBytes); |
| 112 | + // fbData is now cleared as well, it's not a copy |
| 113 | + expect(bytesSum(fbData.buffer.asByteData()), isZero); |
| 114 | + |
| 115 | + // clearing the data must not affect already read objects |
| 116 | + // Note: it previously did because of upstream flatbuffers lazy reading |
| 117 | + checkSameEntities(target, source); |
| 118 | + |
| 119 | + // must be empty after reading again |
| 120 | + checkSameEntities(binding.objectFromFB(env.store, fbData), TestEntity()); |
| 121 | + |
| 122 | + // note: accessing fbData after fb1.clear() is illegal (memory is freed) |
| 123 | + fb1.clear(); |
| 124 | + env.close(); |
| 125 | + |
| 126 | + // clearing the data must not affect already read objects |
| 127 | + checkSameEntities(target, source); |
| 128 | + }); |
41 | 129 | } |
0 commit comments