aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/unit/basic.unit
diff options
context:
space:
mode:
Diffstat (limited to 'c++-libbu++/src/unit/basic.unit')
-rw-r--r--c++-libbu++/src/unit/basic.unit191
1 files changed, 191 insertions, 0 deletions
diff --git a/c++-libbu++/src/unit/basic.unit b/c++-libbu++/src/unit/basic.unit
new file mode 100644
index 0000000..2c2c31a
--- /dev/null
+++ b/c++-libbu++/src/unit/basic.unit
@@ -0,0 +1,191 @@
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/integer.h"
11#include "gats/float.h"
12#include "gats/list.h"
13#include "gats/boolean.h"
14#include "gats/string.h"
15#include "gats/null.h"
16#include "gats/gatsstream.h"
17
18#include "bu/membuf.h"
19#include "bu/list.h"
20#include "bu/sio.h"
21
22#include <stdlib.h>
23#include <math.h>
24
25using namespace Bu;
26
27suite Basic
28{
29 test integer
30 {
31 Bu::List<int64_t> lInts;
32 Bu::MemBuf mb;
33
34 int64_t i = 1;
35 for( int x = 0; x < 256; x++ )
36 {
37 lInts.append( i );
38 Gats::Integer( i ).write( mb );
39 i = -(i<<1)-i;
40 }
41
42 mb.setPos( 0 );
43
44 for( Bu::List<int64_t>::iterator j = lInts.begin(); j; j++ )
45 {
46 Gats::Object *pObj = Gats::Object::read( mb );
47 if( pObj->getType() != Gats::typeInteger )
48 unitFailed("Bad type read.");
49
50 if( dynamic_cast<Gats::Integer *>(pObj)->getValue() != *j )
51 unitFailed("Bad number.");
52 }
53 }
54
55 test string
56 {
57 Bu::List<Bu::String> lStrs;
58 Bu::MemBuf mb;
59
60 lStrs.append( Bu::String() );
61 Gats::String("").write( mb );
62
63 {
64 int iMax = 0;
65 for( int j = 1; j <= (1<<16); j=j<<1 )
66 iMax += j;
67 setStepCount( iMax );
68 }
69 for( int j = 1; j <= (1<<16); j=j<<1 )
70 {
71 Bu::String s( j );
72 for( int x = 0; x < j; x++ )
73 {
74 s[x] = (unsigned char)(random()%256);
75 }
76 incProgress( j );
77 Gats::String( s ).write( mb );
78 lStrs.append( s );
79 }
80
81 mb.setPos( 0 );
82
83 for( Bu::List<Bu::String>::iterator i = lStrs.begin(); i; i++ )
84 {
85 Gats::Object *pObj = Gats::Object::read( mb );
86 if( pObj->getType() != Gats::typeString )
87 unitFailed("Bad type read.");
88
89 if( *dynamic_cast<Gats::String *>(pObj) != *i )
90 unitFailed("Bad string.");
91 }
92 }
93
94 test boolean
95 {
96 Bu::List<bool> lBs;
97 Bu::MemBuf mb;
98
99 for( int j = 0; j < 1024; j++ )
100 {
101 if( random()%2 == 0 )
102 {
103 lBs.append( true );
104 Gats::Boolean( true ).write( mb );
105 }
106 else
107 {
108 lBs.append( false );
109 Gats::Boolean( false ).write( mb );
110 }
111 }
112
113 mb.setPos( 0 );
114
115 for( Bu::List<bool>::iterator i = lBs.begin(); i; i++ )
116 {
117 Gats::Object *pObj = Gats::Object::read( mb );
118 if( pObj->getType() != Gats::typeBoolean )
119 unitFailed("Bad type read.");
120
121 if( dynamic_cast<Gats::Boolean *>(pObj)->getValue() != *i )
122 unitFailed("Bad string.");
123 }
124 }
125
126 test floats
127 {
128 Bu::MemBuf mb;
129
130 Gats::Float( M_PI ).write( mb );
131
132 mb.setPos( 0 );
133
134 Gats::Object *pObj = Gats::Object::read( mb );
135 unitTest( pObj != NULL );
136 unitTest( pObj->getType() == Gats::typeFloat );
137 Gats::Float *pFlt = dynamic_cast<Gats::Float *>(pObj);
138 // sio << "old = " << M_PI << ", new = " << pFlt->getValue() << sio.nl;
139 unitTest( pFlt->getValue() == M_PI );
140
141 delete pObj;
142 }
143
144 test dictionary
145 {
146 MemBuf mb;
147
148 {
149 Gats::Dictionary dict;
150 dict.insert("bool", new Gats::Boolean(true) );
151 dict.insert("int", 33403055 );
152 dict.insert("str", "Hey there" );
153 dict.write( mb );
154 }
155
156 mb.setPos( 0 );
157
158 {
159 Gats::Object *pRead = Gats::Object::read( mb );
160 unitTest( pRead != NULL );
161 Gats::Dictionary *pDict = dynamic_cast<Gats::Dictionary *>(pRead);
162 unitTest( pDict != NULL );
163
164 unitTest( pDict->getBool("bool") == true );
165 unitTest( pDict->getInt("int") == 33403055 );
166 unitTest( pDict->getStr("str") == "Hey there" );
167 unitTest( pDict->getSize() == 3 );
168
169 delete pDict;
170 }
171 }
172
173 test null
174 {
175 MemBuf mb;
176 {
177 Gats::Null n;
178 Gats::GatsStream gs( mb );
179 gs.writeObject( &n );
180 }
181
182 mb.setPos( 0 );
183
184 {
185 Gats::GatsStream gs( mb );
186 Gats::Object *pObj = gs.readObject();
187 unitTest( pObj->getType() == Gats::typeNull );
188 delete pObj;
189 }
190 }
191}