@@ -20,8 +20,13 @@ mod stream_tests {
2020 use fory_core:: buffer:: Reader ;
2121 use fory_core:: stream:: ForyStreamBuf ;
2222 use fory_core:: Fory ;
23+ use std:: fmt:: Debug ;
2324 use std:: io:: Cursor ;
2425
26+ // ========================================================================
27+ // OneByteStream — mirrors C++ OneByteStreamBuf / OneByteIStream
28+ // Delivers exactly 1 byte per read() call for maximum streaming stress.
29+ // ========================================================================
2530 struct OneByte ( Cursor < Vec < u8 > > ) ;
2631 impl std:: io:: Read for OneByte {
2732 fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
@@ -39,22 +44,57 @@ mod stream_tests {
3944 }
4045 }
4146
47+ // ========================================================================
48+ // Deserialize helper — per maintainer requirement:
49+ // "Create a Deserialize help methods in tests, then use that instead of
50+ // fory.Deserialize for deserialization, and in the Deserialize test
51+ // helper, first deserialize from bytes, then wrap it into a
52+ // OneByteStream to deserialize it to ensure deserialization works."
53+ // ========================================================================
54+ fn deserialize_helper < T > ( fory : & Fory , bytes : & [ u8 ] ) -> T
55+ where
56+ T : fory_core:: Serializer + fory_core:: ForyDefault + PartialEq + Debug ,
57+ {
58+ // Path 1: deserialize from bytes (standard in-memory path)
59+ let from_bytes: T = fory. deserialize ( bytes) . expect ( "bytes deserialize failed" ) ;
60+
61+ // Path 2: deserialize from OneByteStream (streaming path)
62+ let from_stream: T = fory
63+ . deserialize_from_stream ( OneByte ( Cursor :: new ( bytes. to_vec ( ) ) ) )
64+ . expect ( "stream deserialize failed" ) ;
65+
66+ // Assert both paths produce the same result
67+ assert_eq ! (
68+ from_bytes, from_stream,
69+ "bytes vs stream deserialization mismatch"
70+ ) ;
71+
72+ from_bytes
73+ }
74+
75+ // ========================================================================
76+ // Test: PrimitiveAndStringRoundTrip
77+ // Mirrors C++ StreamSerializationTest::PrimitiveAndStringRoundTrip
78+ // ========================================================================
4279 #[ test]
43- fn test_primitive_stream_roundtrip ( ) {
80+ fn test_primitive_and_string_round_trip ( ) {
4481 let fory = Fory :: default ( ) ;
82+
83+ // i64 round-trip
4584 let bytes = fory. serialize ( & -9876543212345i64 ) . unwrap ( ) ;
46- let result: i64 = fory
47- . deserialize_from_stream ( OneByte ( Cursor :: new ( bytes) ) )
48- . unwrap ( ) ;
85+ let result = deserialize_helper :: < i64 > ( & fory, & bytes) ;
4986 assert_eq ! ( result, -9876543212345i64 ) ;
5087
88+ // String round-trip (with unicode)
5189 let bytes = fory. serialize ( & "stream-hello-世界" . to_string ( ) ) . unwrap ( ) ;
52- let result: String = fory
53- . deserialize_from_stream ( OneByte ( Cursor :: new ( bytes) ) )
54- . unwrap ( ) ;
90+ let result = deserialize_helper :: < String > ( & fory, & bytes) ;
5591 assert_eq ! ( result, "stream-hello-世界" ) ;
5692 }
5793
94+ // ========================================================================
95+ // Test: SequentialDeserializeFromSingleStream
96+ // Mirrors C++ StreamSerializationTest::SequentialDeserializeFromSingleStream
97+ // ========================================================================
5898 #[ test]
5999 fn test_sequential_stream_reads ( ) {
60100 let fory = Fory :: default ( ) ;
@@ -74,6 +114,10 @@ mod stream_tests {
74114 assert_eq ! ( third, 99 ) ;
75115 }
76116
117+ // ========================================================================
118+ // Test: TruncatedStreamReturnsError
119+ // Mirrors C++ StreamSerializationTest::TruncatedStreamReturnsError
120+ // ========================================================================
77121 #[ test]
78122 fn test_truncated_stream_returns_error ( ) {
79123 let fory = Fory :: default ( ) ;
@@ -82,4 +126,59 @@ mod stream_tests {
82126 let result: Result < String , _ > = fory. deserialize_from_stream ( Cursor :: new ( bytes) ) ;
83127 assert ! ( result. is_err( ) ) ;
84128 }
129+
130+ // ========================================================================
131+ // Test: ShrinkBuffer compacts consumed bytes
132+ // Validates the C++ shrink_buffer() behavior is correctly implemented
133+ // ========================================================================
134+ #[ test]
135+ fn test_shrink_buffer_compacts_consumed_bytes ( ) {
136+ let fory = Fory :: default ( ) ;
137+
138+ // Serialize multiple values into a single buffer
139+ let mut bytes = Vec :: new ( ) ;
140+ fory. serialize_to ( & mut bytes, & 42i32 ) . unwrap ( ) ;
141+ fory. serialize_to ( & mut bytes, & "shrink-test" . to_string ( ) )
142+ . unwrap ( ) ;
143+ fory. serialize_to ( & mut bytes, & 100i64 ) . unwrap ( ) ;
144+
145+ // Use a small initial buffer to force multiple fills
146+ let mut reader =
147+ Reader :: from_stream ( ForyStreamBuf :: with_capacity ( OneByte ( Cursor :: new ( bytes) ) , 4 ) ) ;
148+
149+ // After each deserialize_from, shrink_buffer should compact the stream.
150+ let first: i32 = fory. deserialize_from ( & mut reader) . unwrap ( ) ;
151+ assert_eq ! ( first, 42 ) ;
152+
153+ let second: String = fory. deserialize_from ( & mut reader) . unwrap ( ) ;
154+ assert_eq ! ( second, "shrink-test" ) ;
155+
156+ let third: i64 = fory. deserialize_from ( & mut reader) . unwrap ( ) ;
157+ assert_eq ! ( third, 100 ) ;
158+ }
159+
160+ // ========================================================================
161+ // Test: Additional primitive types through deserialize_helper
162+ // ========================================================================
163+ #[ test]
164+ fn test_additional_primitive_types ( ) {
165+ let fory = Fory :: default ( ) ;
166+
167+ // bool
168+ let bytes = fory. serialize ( & true ) . unwrap ( ) ;
169+ assert_eq ! ( deserialize_helper:: <bool >( & fory, & bytes) , true ) ;
170+
171+ // i32
172+ let bytes = fory. serialize ( & -42i32 ) . unwrap ( ) ;
173+ assert_eq ! ( deserialize_helper:: <i32 >( & fory, & bytes) , -42i32 ) ;
174+
175+ // f64
176+ let bytes = fory. serialize ( & 3.14159f64 ) . unwrap ( ) ;
177+ assert_eq ! ( deserialize_helper:: <f64 >( & fory, & bytes) , 3.14159f64 ) ;
178+
179+ // Vec<i32>
180+ let vec = vec ! [ 1i32 , 2 , 3 , 5 , 8 ] ;
181+ let bytes = fory. serialize ( & vec) . unwrap ( ) ;
182+ assert_eq ! ( deserialize_helper:: <Vec <i32 >>( & fory, & bytes) , vec) ;
183+ }
85184}
0 commit comments