Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions dart/packages/fory-test/lib/entity/uint_struct.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import 'package:fory/fory.dart';

part '../generated/uint_struct.g.dart';

@ForyClass(promiseAcyclic: true)
class UIntStruct with _$UIntStructFory {
final UInt8 age;
final UInt16 port;
final UInt32 count;

const UIntStruct(
this.age,
this.port,
this.count,
);

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other is UIntStruct &&
runtimeType == other.runtimeType &&
age == other.age &&
port == other.port &&
count == other.count);
}

@override
int get hashCode => age.hashCode ^ port.hashCode ^ count.hashCode;
}
86 changes: 86 additions & 0 deletions dart/packages/fory-test/test/struct_test/uint_struct_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

library;

import 'package:fory/fory.dart';
import 'package:fory_test/entity/uint_struct.dart';
import 'package:test/test.dart';

void main() {
group('UInt struct serialization', () {
test('serializes and deserializes UInt fields correctly', () {
// Create test instance
var original = UIntStruct(
UInt8(25),
UInt16(8080),
UInt32(1000000),
);

// Serialize
var fory = Fory();
fory.register($UIntStruct);
var bytes = fory.toFory(original);

expect(bytes.isNotEmpty, isTrue);

// Deserialize
var decoded = fory.fromFory(bytes) as UIntStruct;

// Verify values
expect(decoded.age.value, 25);
expect(decoded.port.value, 8080);
expect(decoded.count.value, 1000000);
});

test('handles max values correctly', () {
var original = UIntStruct(
UInt8(255), // Max UInt8
UInt16(65535), // Max UInt16
UInt32(4294967295), // Max UInt32
);

var fory = Fory();
fory.register($UIntStruct);
var bytes = fory.toFory(original);
var decoded = fory.fromFory(bytes) as UIntStruct;

expect(decoded.age.value, 255);
expect(decoded.port.value, 65535);
expect(decoded.count.value, 4294967295);
});

test('handles min values correctly', () {
var original = UIntStruct(
UInt8(0),
UInt16(0),
UInt32(0),
);

var fory = Fory();
fory.register($UIntStruct);
var bytes = fory.toFory(original);
var decoded = fory.fromFory(bytes) as UIntStruct;

expect(decoded.age.value, 0);
expect(decoded.port.value, 0);
expect(decoded.count.value, 0);
});
});
}
6 changes: 6 additions & 0 deletions dart/packages/fory/lib/src/const/dart_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import 'package:fory/src/datatype/float32.dart';
import 'package:fory/src/datatype/int16.dart';
import 'package:fory/src/datatype/int32.dart';
import 'package:fory/src/datatype/int8.dart';
import 'package:fory/src/datatype/uint8.dart';
import 'package:fory/src/datatype/uint16.dart';
import 'package:fory/src/datatype/uint32.dart';

import 'package:fory/src/const/obj_type.dart';
import 'package:fory/src/datatype/local_date.dart';
Expand All @@ -41,6 +44,9 @@ enum DartTypeEnum{
INT8(Int8, true, 'Int8', 'package', 'fory/src/datatype/int8.dart', ObjType.INT8, true, 'dart:core@Int8'),
INT16(Int16, true, 'Int16', 'package', 'fory/src/datatype/int16.dart', ObjType.INT16, true, 'dart:core@Int16'),
INT32(Int32, true, 'Int32', 'package', 'fory/src/datatype/int32.dart', ObjType.INT32, true, 'dart:core@Int32'),
UINT8(UInt8, true, 'UInt8', 'package', 'fory/src/datatype/uint8.dart', ObjType.UINT8, true, 'dart:core@UInt8'),
UINT16(UInt16, true, 'UInt16', 'package', 'fory/src/datatype/uint16.dart', ObjType.UINT16, true, 'dart:core@UInt16'),
UINT32(UInt32, true, 'UInt32', 'package', 'fory/src/datatype/uint32.dart', ObjType.UINT32, true, 'dart:core@UInt32'),
INT(int,true, 'int', 'dart', 'core', ObjType.INT64, true, 'dart:core@int'),
FLOAT32(Float32, true, 'Float32', 'package', 'fory/src/datatype/float32.dart', ObjType.FLOAT32, true, 'dart:core@Float32'),
DOUBLE(double,true, 'double', 'dart', 'core', ObjType.FLOAT64, true, 'dart:core@double'),
Expand Down
6 changes: 6 additions & 0 deletions dart/packages/fory/lib/src/serializer/serializer_pool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import 'package:fory/src/datatype/float32.dart';
import 'package:fory/src/datatype/int16.dart';
import 'package:fory/src/datatype/int32.dart';
import 'package:fory/src/datatype/int8.dart';
import 'package:fory/src/datatype/uint8.dart';
import 'package:fory/src/datatype/uint16.dart';
import 'package:fory/src/datatype/uint32.dart';
import 'package:fory/src/datatype/local_date.dart';
import 'package:fory/src/datatype/timestamp.dart';
import 'package:fory/src/meta/type_info.dart';
Expand Down Expand Up @@ -63,6 +66,9 @@ class SerializerPool{
type2Ser[Int8]!.ser = Int8Serializer.cache.getSerializer(conf);
type2Ser[Int16]!.ser = Int16Serializer.cache.getSerializer(conf);
type2Ser[Int32]!.ser = Int32Serializer.cache.getSerializer(conf);
type2Ser[UInt8]!.ser = UInt8Serializer.cache.getSerializer(conf);
type2Ser[UInt16]!.ser = UInt16Serializer.cache.getSerializer(conf);
type2Ser[UInt32]!.ser = UInt32Serializer.cache.getSerializer(conf);
type2Ser[Float32]!.ser = Float32Serializer.cache.getSerializer(conf);
type2Ser[String]!.ser = StringSerializer.cache.getSerializer(conf);

Expand Down
Loading