aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/unit/io.unit
blob: 1a9747f7c7a30b3447ffe7b980f6f57f9eaf86f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// vim: syntax=cpp
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "gats/dictionary.h"
#include "gats/integer.h"
#include "gats/float.h"
#include "gats/list.h"
#include "gats/boolean.h"
#include "gats/string.h"
#include "gats/gatsstream.h"

#include "bu/membuf.h"
#include "bu/list.h"
#include "bu/sio.h"

#include <stdlib.h>

using namespace Bu;

suite Basic
{
	test basic
	{
		Bu::String sTmpFileName("temp-XXXXXXXXX");
		Bu::File fIo = tempFile( sTmpFileName );

		{
			Gats::Dictionary dTest;
			dTest.insert("age", 27 );
			dTest.insert("firstName", "Mike");
			dTest.insert("lastName", "Buland");
			dTest.insert("awake", true );
			
			Gats::GatsStream sGats( fIo );
			sGats.writeObject( &dTest );
		}

		fIo.setPos( 0 );

		{
			Gats::GatsStream sGats( fIo );
			Gats::Object *pObj = sGats.readObject();
			unitTest( pObj != NULL );
			unitTest( pObj->getType() == Gats::typeDictionary );
			Gats::Dictionary *pDic = dynamic_cast<Gats::Dictionary *>(pObj);
			unitTest( pDic->getSize() == 4 );
			unitTest( pDic->getInt("age") == 27 );
			unitTest( pDic->getStr("firstName") == "Mike" );
			unitTest( pDic->getStr("lastName") == "Buland" );
			unitTest( pDic->getBool("awake") == true );

			delete pDic;
		}
	}

	test spacers
	{
		Bu::String sTmpFileName("temp-XXXXXXXXX");
		Bu::File fIo = tempFile( sTmpFileName );

		{
			Gats::GatsStream sGats( fIo );
			Gats::Integer i( -157 );
			sGats.writeObject( &i );
			fIo.write( "\x00\x00\x00", 3 );
			Gats::String s("negative one hundred and fifty seven");
			sGats.writeObject( &s );
		}

		fIo.setPos( 0 );

		{
			Gats::GatsStream sGats( fIo );
			Gats::Object *pObj1 = sGats.readObject();
			unitTest( pObj1 != NULL );
			unitTest( pObj1->getType() == Gats::typeInteger );
			unitTest( dynamic_cast<Gats::Integer *>(pObj1)->getValue() == -157 );

			Gats::Object *pObj2 = sGats.readObject();
			unitTest( pObj2 != NULL );
			unitTest( pObj2->getType() == Gats::typeString );
			unitTest( *dynamic_cast<Gats::String *>(pObj2) ==
				"negative one hundred and fifty seven" );

			delete pObj1;
			delete pObj2;
		}
	}

	test biggerSpacers
	{
		Bu::String sTmpFileName("temp-XXXXXXXXX");
		Bu::File fIo = tempFile( sTmpFileName );

		{
			Gats::GatsStream sGats( fIo );
			Gats::Integer i( -157 );
			sGats.writeObject( &i );
			fIo.write( "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9 );
			Gats::String s("negative one hundred and fifty seven");
			sGats.writeObject( &s );
		}

		fIo.setPos( 0 );

		{
			Gats::GatsStream sGats( fIo );
			Gats::Object *pObj1 = sGats.readObject();
			unitTest( pObj1 != NULL );
			unitTest( pObj1->getType() == Gats::typeInteger );
			unitTest( dynamic_cast<Gats::Integer *>(pObj1)->getValue() == -157 );

			Gats::Object *pObj2 = sGats.readObject();
			unitTest( pObj2 != NULL );
			unitTest( pObj2->getType() == Gats::typeString );
			unitTest( *dynamic_cast<Gats::String *>(pObj2) ==
				"negative one hundred and fifty seven" );

			delete pObj1;
			delete pObj2;
		}
	}
}