aboutsummaryrefslogtreecommitdiff
path: root/src/unit/io.unit
diff options
context:
space:
mode:
Diffstat (limited to 'src/unit/io.unit')
-rw-r--r--src/unit/io.unit127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/unit/io.unit b/src/unit/io.unit
new file mode 100644
index 0000000..3e9c82c
--- /dev/null
+++ b/src/unit/io.unit
@@ -0,0 +1,127 @@
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/gatsstream.h"
16
17#include "bu/membuf.h"
18#include "bu/list.h"
19#include "bu/sio.h"
20
21#include <stdlib.h>
22
23using namespace Bu;
24
25suite Basic
26{
27 test basic
28 {
29 Bu::FString sTmpFileName("temp-XXXXXXXXX");
30 Bu::File fIo = tempFile( sTmpFileName );
31
32 {
33 Gats::Dictionary dTest;
34 dTest.insert("age", 27 );
35 dTest.insert("firstName", "Mike");
36 dTest.insert("lastName", "Buland");
37 dTest.insert("awake", true );
38
39 Gats::GatsStream sGats( fIo );
40 sGats.writeObject( &dTest );
41 }
42
43 fIo.setPos( 0 );
44
45 {
46 Gats::GatsStream sGats( fIo );
47 Gats::Object *pObj = sGats.readObject();
48 unitTest( pObj != NULL );
49 unitTest( pObj->getType() == Gats::typeDictionary );
50 Gats::Dictionary *pDic = dynamic_cast<Gats::Dictionary *>(pObj);
51 unitTest( pDic->getSize() == 4 );
52 unitTest( pDic->getInt("age") == 27 );
53 unitTest( pDic->getStr("firstName") == "Mike" );
54 unitTest( pDic->getStr("lastName") == "Buland" );
55 unitTest( pDic->getBool("awake") == true );
56
57 delete pDic;
58 }
59 }
60
61 test spacers
62 {
63 Bu::FString sTmpFileName("temp-XXXXXXXXX");
64 Bu::File fIo = tempFile( sTmpFileName );
65
66 {
67 Gats::GatsStream sGats( fIo );
68 Gats::Integer i( -157 );
69 sGats.writeObject( &i );
70 fIo.write( "\x00\x00\x00", 3 );
71 Gats::String s("negative one hundred and fifty seven");
72 sGats.writeObject( &s );
73 }
74
75 fIo.setPos( 0 );
76
77 {
78 Gats::GatsStream sGats( fIo );
79 Gats::Object *pObj1 = sGats.readObject();
80 unitTest( pObj1 != NULL );
81 unitTest( pObj1->getType() == Gats::typeInteger );
82 unitTest( dynamic_cast<Gats::Integer *>(pObj1)->getValue() == -157 );
83
84 Gats::Object *pObj2 = sGats.readObject();
85 unitTest( pObj2 != NULL );
86 unitTest( pObj2->getType() == Gats::typeString );
87 unitTest( *dynamic_cast<Gats::String *>(pObj2) ==
88 "negative one hundred and fifty seven" );
89
90 delete pObj1;
91 delete pObj2;
92 }
93 }
94 test biggerSpacers
95 {
96 Bu::FString sTmpFileName("temp-XXXXXXXXX");
97 Bu::File fIo = tempFile( sTmpFileName );
98
99 {
100 Gats::GatsStream sGats( fIo );
101 Gats::Integer i( -157 );
102 sGats.writeObject( &i );
103 fIo.write( "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9 );
104 Gats::String s("negative one hundred and fifty seven");
105 sGats.writeObject( &s );
106 }
107
108 fIo.setPos( 0 );
109
110 {
111 Gats::GatsStream sGats( fIo );
112 Gats::Object *pObj1 = sGats.readObject();
113 unitTest( pObj1 != NULL );
114 unitTest( pObj1->getType() == Gats::typeInteger );
115 unitTest( dynamic_cast<Gats::Integer *>(pObj1)->getValue() == -157 );
116
117 Gats::Object *pObj2 = sGats.readObject();
118 unitTest( pObj2 != NULL );
119 unitTest( pObj2->getType() == Gats::typeString );
120 unitTest( *dynamic_cast<Gats::String *>(pObj2) ==
121 "negative one hundred and fifty seven" );
122
123 delete pObj1;
124 delete pObj2;
125 }
126 }
127}