diff options
Diffstat (limited to 'c++-libbu++/src/unit/float.unit')
| -rw-r--r-- | c++-libbu++/src/unit/float.unit | 84 | 
1 files changed, 84 insertions, 0 deletions
| diff --git a/c++-libbu++/src/unit/float.unit b/c++-libbu++/src/unit/float.unit new file mode 100644 index 0000000..b1eb063 --- /dev/null +++ b/c++-libbu++/src/unit/float.unit | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | // vim: syntax=cpp | ||
| 2 | /* | ||
| 3 | * Copyright (C) 2007-2010 Xagasoft, All rights reserved. | ||
| 4 | * | ||
| 5 | * This file is part of the libbu++ library and is released under the | ||
| 6 | * terms of the license contained in the file LICENSE. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "gats/dictionary.h" | ||
| 10 | #include "gats/float.h" | ||
| 11 | #include "gats/string.h" | ||
| 12 | |||
| 13 | #include "bu/membuf.h" | ||
| 14 | #include "bu/list.h" | ||
| 15 | #include "bu/sio.h" | ||
| 16 | |||
| 17 | #include <stdlib.h> | ||
| 18 | #include <math.h> | ||
| 19 | |||
| 20 | using namespace Bu; | ||
| 21 | |||
| 22 | suite Basic | ||
| 23 | { | ||
| 24 | void rw( double dVal ) | ||
| 25 | { | ||
| 26 | Bu::MemBuf mb; | ||
| 27 | |||
| 28 | Gats::Float( dVal ).write( mb ); | ||
| 29 | |||
| 30 | mb.setPos( 0 ); | ||
| 31 | |||
| 32 | Gats::Object *pObj = Gats::Object::read( mb ); | ||
| 33 | unitTest( pObj != NULL ); | ||
| 34 | unitTest( pObj->getType() == Gats::typeFloat ); | ||
| 35 | Gats::Float *pFlt = dynamic_cast<Gats::Float *>(pObj); | ||
| 36 | Bu::String sHex; | ||
| 37 | for( Bu::String::iterator i = mb.getString().begin(); i; i++ ) | ||
| 38 | { | ||
| 39 | sHex += "0123456789abcdef"[(((uint8_t)*i)>>4)&0x0f]; | ||
| 40 | sHex += "0123456789abcdef"[(*i)&0x0f]; | ||
| 41 | sHex += ' '; | ||
| 42 | } | ||
| 43 | printf("In: %a\nOut: %a\nRaw: %s\n", dVal, pFlt->getValue(), sHex.getStr() ); | ||
| 44 | if( isnan( dVal ) ) | ||
| 45 | unitTest( isnan(pFlt->getValue()) == isnan(dVal) ); | ||
| 46 | else | ||
| 47 | unitTest( pFlt->getValue() == dVal ); | ||
| 48 | unitTest( signbit(pFlt->getValue()) == signbit(dVal) ); | ||
| 49 | |||
| 50 | delete pObj; | ||
| 51 | } | ||
| 52 | |||
| 53 | test positive | ||
| 54 | { | ||
| 55 | rw( 8485738457.0 ); | ||
| 56 | rw( 63723.0 ); | ||
| 57 | rw( 0.000000000000001928173 ); | ||
| 58 | rw( 1.0 ); | ||
| 59 | rw( 0.0 ); | ||
| 60 | rw( M_PI ); | ||
| 61 | } | ||
| 62 | |||
| 63 | test negitave | ||
| 64 | { | ||
| 65 | rw( -8485738457.0 ); | ||
| 66 | rw( -63723.0 ); | ||
| 67 | rw( -0.000000000000001928173 ); | ||
| 68 | rw( -1.0 ); | ||
| 69 | rw( -0.0 ); | ||
| 70 | rw( -M_PI ); | ||
| 71 | } | ||
| 72 | |||
| 73 | test inf | ||
| 74 | { | ||
| 75 | rw( INFINITY ); | ||
| 76 | rw( -INFINITY ); | ||
| 77 | } | ||
| 78 | |||
| 79 | test nan | ||
| 80 | { | ||
| 81 | rw( NAN ); | ||
| 82 | rw( -NAN ); | ||
| 83 | } | ||
| 84 | } | ||
