diff options
Diffstat (limited to 'c++-libbu++/src/unit/io.unit')
-rw-r--r-- | c++-libbu++/src/unit/io.unit | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/c++-libbu++/src/unit/io.unit b/c++-libbu++/src/unit/io.unit new file mode 100644 index 0000000..1a9747f --- /dev/null +++ b/c++-libbu++/src/unit/io.unit | |||
@@ -0,0 +1,128 @@ | |||
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 | |||
23 | using namespace Bu; | ||
24 | |||
25 | suite Basic | ||
26 | { | ||
27 | test basic | ||
28 | { | ||
29 | Bu::String 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::String 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 | |||
95 | test biggerSpacers | ||
96 | { | ||
97 | Bu::String sTmpFileName("temp-XXXXXXXXX"); | ||
98 | Bu::File fIo = tempFile( sTmpFileName ); | ||
99 | |||
100 | { | ||
101 | Gats::GatsStream sGats( fIo ); | ||
102 | Gats::Integer i( -157 ); | ||
103 | sGats.writeObject( &i ); | ||
104 | fIo.write( "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9 ); | ||
105 | Gats::String s("negative one hundred and fifty seven"); | ||
106 | sGats.writeObject( &s ); | ||
107 | } | ||
108 | |||
109 | fIo.setPos( 0 ); | ||
110 | |||
111 | { | ||
112 | Gats::GatsStream sGats( fIo ); | ||
113 | Gats::Object *pObj1 = sGats.readObject(); | ||
114 | unitTest( pObj1 != NULL ); | ||
115 | unitTest( pObj1->getType() == Gats::typeInteger ); | ||
116 | unitTest( dynamic_cast<Gats::Integer *>(pObj1)->getValue() == -157 ); | ||
117 | |||
118 | Gats::Object *pObj2 = sGats.readObject(); | ||
119 | unitTest( pObj2 != NULL ); | ||
120 | unitTest( pObj2->getType() == Gats::typeString ); | ||
121 | unitTest( *dynamic_cast<Gats::String *>(pObj2) == | ||
122 | "negative one hundred and fifty seven" ); | ||
123 | |||
124 | delete pObj1; | ||
125 | delete pObj2; | ||
126 | } | ||
127 | } | ||
128 | } | ||