|
| 1 | +#include "fbcunit.bi" |
| 2 | + |
| 3 | +'' show the compared numbers, but only if fbcu.getHideCases() is false |
| 4 | +#undef CU_ASSERT_EQUAL |
| 5 | +#define CU_ASSERT_EQUAL(a, b) if ((a) <> (b)) and (fbcu.getHideCases() = false) then print (a), (b) end if: CU_ASSERT( (a) = (b) ) |
| 6 | + |
| 7 | +SUITE( fbc_tests.numbers.cast_f2l ) |
| 8 | + |
| 9 | + sub checkcast(byval x as double, byval ul as ulong) |
| 10 | + |
| 11 | + '' 32 bit |
| 12 | + CU_ASSERT_EQUAL( cuint( x ), cuint( ul ) ) |
| 13 | + |
| 14 | + if( ul > 1ul shl 31 ) then return |
| 15 | + CU_ASSERT_EQUAL( cint( -x ), cint( -ul ) ) |
| 16 | + CU_ASSERT_EQUAL( clng( -x ), clng( -ul ) ) |
| 17 | + |
| 18 | + if( ul > (1ul shl 31) - 1 ) then return |
| 19 | + CU_ASSERT_EQUAL( cint( x ), cint( ul ) ) |
| 20 | + CU_ASSERT_EQUAL( clng( x ), clng( ul ) ) |
| 21 | + |
| 22 | + if( ul > (1ul shl 31) - 1 ) then return |
| 23 | + CU_ASSERT_EQUAL( cint( x ), cint( ul ) ) |
| 24 | + CU_ASSERT_EQUAL( clng( x ), clng( ul ) ) |
| 25 | + |
| 26 | + '' 16 bit |
| 27 | + if( ul > 65535 ) then return |
| 28 | + CU_ASSERT_EQUAL( cushort( x ), cushort( ul ) ) |
| 29 | + |
| 30 | + if( ul > 32768 ) then return |
| 31 | + CU_ASSERT_EQUAL( cshort( -x ), cshort( -ul ) ) |
| 32 | + |
| 33 | + if( ul > 32767 ) then return |
| 34 | + CU_ASSERT_EQUAL( cshort( x ), cshort( ul ) ) |
| 35 | + |
| 36 | + '' 8 bit |
| 37 | + if( ul > 255 ) then return |
| 38 | + CU_ASSERT_EQUAL( cubyte( x ), cubyte( ul ) ) |
| 39 | + |
| 40 | + if( ul > 128 ) then return |
| 41 | + CU_ASSERT_EQUAL( cbyte( -x ), cbyte( -ul ) ) |
| 42 | + |
| 43 | + if( ul > 127 ) then return |
| 44 | + CU_ASSERT_EQUAL( cbyte( x ), cbyte( ul ) ) |
| 45 | + |
| 46 | + end sub |
| 47 | + |
| 48 | + sub testnum( byval n as ulong ) |
| 49 | + |
| 50 | + #define lsb(n) ((n) and -(n)) '' keep only least significant bit |
| 51 | + |
| 52 | + '' only run when n has <= 53 significant bits |
| 53 | + if( n and -(lsb(n) shl 53) ) then return |
| 54 | + |
| 55 | + dim x as double = cdbl(n) |
| 56 | + |
| 57 | + checkcast( x, n ) |
| 58 | + |
| 59 | + if( n < 1ul shl 31 ) then |
| 60 | + |
| 61 | + '' make sure that cdbl(l) concurs with cdbl(ul) |
| 62 | + CU_ASSERT_EQUAL( x, cdbl(clng(n)) ) |
| 63 | + |
| 64 | + |
| 65 | + if( n < 1ul shl 52 ) then |
| 66 | + checkcast( x + 0.5, n + (n and 1) ) |
| 67 | + checkcast( x - 0.5, n - (n and 1) ) |
| 68 | + end if |
| 69 | + |
| 70 | + if( n < 1ul shl 51 ) then |
| 71 | + checkcast( x + 0.25, n ) |
| 72 | + checkcast( x - 0.25, n ) |
| 73 | + end if |
| 74 | + |
| 75 | + end if |
| 76 | + |
| 77 | + end sub |
| 78 | + |
| 79 | + TEST( cast_l ) |
| 80 | + |
| 81 | + dim as long n = 1l |
| 82 | + dim as double x = 1.0 |
| 83 | + |
| 84 | + dim as integer i, j, k, l |
| 85 | + |
| 86 | + '' test powers of 2 |
| 87 | + for i = 0 to 31 |
| 88 | + testnum( 1ul shl i ) |
| 89 | + next i |
| 90 | + |
| 91 | + '' test various bit combinations |
| 92 | + for i = 0 to 31 |
| 93 | + for j = 0 to 31 |
| 94 | + testnum( (1 shl i) + (1 shl j) ) |
| 95 | + next j |
| 96 | + next i |
| 97 | + |
| 98 | + END_TEST |
| 99 | + |
| 100 | +END_SUITE |
0 commit comments