@@ -175,9 +175,6 @@ void test_truncate_fp_to_int(void) {
175175 const IntConversionTestCase * test_case ;
176176 int i ;
177177 int result ;
178- Float less_than_one = { 0x00000000 , 127 };
179- Float too_large = { 0x00000000 , 144 };
180- Float much_too_large = { 0x00000000 , 197 };
181178
182179 PRINT_TEST_NAME ();
183180
@@ -194,18 +191,53 @@ void test_truncate_fp_to_int(void) {
194191 // Some unusual cases.
195192
196193 // If floating point value is less than 1, then output is zero.
197- load_fp0 (& less_than_one );
198- result = truncate_fp_to_int ();
199- ASSERT_EQ (err , 0 );
200- ASSERT_EQ (result , 0 );
194+ {
195+ Float less_than_one = { 0x00000000 , 127 };
196+ load_fp0 (& less_than_one );
197+ result = truncate_fp_to_int ();
198+ ASSERT_EQ (err , 0 );
199+ ASSERT_EQ (result , 0 );
200+ }
201201
202- // If floating point value is >=2^16, the function should return error.
203- load_fp0 (& too_large );
204- result = truncate_fp_to_int ();
205- ASSERT_NE (err , 0 );
206- load_fp0 (& much_too_large );
207- result = truncate_fp_to_int ();
208- ASSERT_NE (err , 0 );
202+ // 32768 (positive) should succeed. On cc65 the 16-bit int wraps to -32768.
203+ {
204+ Float fp_32768 = { 0x00000000 , 143 }; // +32768
205+ load_fp0 (& fp_32768 );
206+ result = truncate_fp_to_int ();
207+ ASSERT_EQ (err , 0 );
208+ ASSERT_EQ (result , (int )-32768L );
209+ }
210+
211+ // 65535 should succeed. On cc65 the 16-bit int wraps to -1.
212+ {
213+ Float fp_65535 = { 0x7FFF0000 , 143 }; // 65535
214+ load_fp0 (& fp_65535 );
215+ result = truncate_fp_to_int ();
216+ ASSERT_EQ (err , 0 );
217+ ASSERT_EQ (result , -1 );
218+ }
219+
220+ // If floating point value is >=2^16 (65536), the function should return error.
221+ {
222+ Float too_large = { 0x00000000 , 144 }; // 65536
223+ Float much_too_large = { 0x00000000 , 197 };
224+ load_fp0 (& too_large );
225+ result = truncate_fp_to_int ();
226+ ASSERT_NE (err , 0 );
227+ load_fp0 (& much_too_large );
228+ result = truncate_fp_to_int ();
229+ ASSERT_NE (err , 0 );
230+ }
231+
232+ // -32769 should succeed (magnitude 32769 fits in 16 bits).
233+ // 16-bit negation of 0x8001 = 0x7FFF = 32767.
234+ {
235+ Float fp_neg_32769 = { 0x80010000 , 143 }; // -32769
236+ load_fp0 (& fp_neg_32769 );
237+ result = truncate_fp_to_int ();
238+ ASSERT_EQ (err , 0 );
239+ ASSERT_EQ (result , 32767 );
240+ }
209241}
210242
211243typedef struct Int32ConversionTestCase {
0 commit comments