diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-03-08 17:49:23 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-03-08 17:49:23 +0000 |
commit | cd210c95a5a429293aa5c88965d3526116ba8723 (patch) | |
tree | 0a3d42cbdfa9f47c735d5436adf129616105eb44 /src/unit | |
parent | a5009d2e8c0378180dd3aca39c10c1dc0af3a93e (diff) | |
download | libgats-cd210c95a5a429293aa5c88965d3526116ba8723.tar.gz libgats-cd210c95a5a429293aa5c88965d3526116ba8723.tar.bz2 libgats-cd210c95a5a429293aa5c88965d3526116ba8723.tar.xz libgats-cd210c95a5a429293aa5c88965d3526116ba8723.zip |
The new float format is in place. The encoder/decoder may not be as fast right
now as it could be, but it is universal, which is preferable in many cases.
We effectively use a normalized base-256 format to store the number, with a
scale, also with a base of 256. Basically, with x86 doubles, the C99 standard
textual, lossless hex encoding format is at max 23 bytes. This encoding is
equivelent but at max 11 bytes, including the format specifier ('f').
Diffstat (limited to 'src/unit')
-rw-r--r-- | src/unit/basic.unit | 2 | ||||
-rw-r--r-- | src/unit/float.unit | 75 |
2 files changed, 76 insertions, 1 deletions
diff --git a/src/unit/basic.unit b/src/unit/basic.unit index 369e095..3ab8dc2 100644 --- a/src/unit/basic.unit +++ b/src/unit/basic.unit | |||
@@ -145,7 +145,7 @@ suite Basic | |||
145 | 145 | ||
146 | { | 146 | { |
147 | Gats::Dictionary dict; | 147 | Gats::Dictionary dict; |
148 | dict.insert("bool", true ); | 148 | dict.insert("bool", new Gats::Boolean(true) ); |
149 | dict.insert("int", 33403055 ); | 149 | dict.insert("int", 33403055 ); |
150 | dict.insert("str", "Hey there" ); | 150 | dict.insert("str", "Hey there" ); |
151 | dict.write( mb ); | 151 | dict.write( mb ); |
diff --git a/src/unit/float.unit b/src/unit/float.unit new file mode 100644 index 0000000..0473ffb --- /dev/null +++ b/src/unit/float.unit | |||
@@ -0,0 +1,75 @@ | |||
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 | // printf("In: %a\nOut: %a\n", dVal, pFlt->getValue() ); | ||
37 | if( isnan( dVal ) ) | ||
38 | unitTest( isnan(pFlt->getValue()) == isnan(dVal) ); | ||
39 | else | ||
40 | unitTest( pFlt->getValue() == dVal ); | ||
41 | unitTest( signbit(pFlt->getValue()) == signbit(dVal) ); | ||
42 | |||
43 | delete pObj; | ||
44 | } | ||
45 | |||
46 | test positive | ||
47 | { | ||
48 | rw( 8485738457.0 ); | ||
49 | rw( 0.000000000000001928173 ); | ||
50 | rw( 1.0 ); | ||
51 | rw( 0.0 ); | ||
52 | rw( M_PI ); | ||
53 | } | ||
54 | |||
55 | test negitave | ||
56 | { | ||
57 | rw( -8485738457.0 ); | ||
58 | rw( -0.000000000000001928173 ); | ||
59 | rw( -1.0 ); | ||
60 | rw( -0.0 ); | ||
61 | rw( -M_PI ); | ||
62 | } | ||
63 | |||
64 | test inf | ||
65 | { | ||
66 | rw( INFINITY ); | ||
67 | rw( -INFINITY ); | ||
68 | } | ||
69 | |||
70 | test nan | ||
71 | { | ||
72 | rw( NAN ); | ||
73 | rw( -NAN ); | ||
74 | } | ||
75 | } | ||