xref: /haiku/src/tests/kits/app/bmessage/MessageInt32ItemTest.cpp (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 //------------------------------------------------------------------------------
2 //	MessageInt32ItemTest.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <stdio.h>
8 #include <posix/string.h>
9 
10 // System Includes -------------------------------------------------------------
11 #include <Message.h>
12 
13 // Project Includes ------------------------------------------------------------
14 
15 // Local Includes --------------------------------------------------------------
16 #include "MessageInt32ItemTest.h"
17 
18 // Local Defines ---------------------------------------------------------------
19 
20 // Globals ---------------------------------------------------------------------
21 
22 #ifndef USE_TEMPLATES
23 //------------------------------------------------------------------------------
24 void TMessageInt32ItemTest::MessageInt32ItemTest1()
25 {
26 	BMessage msg;
27 	int32 out = 0;
28 	CPPUNIT_ASSERT(msg.FindInt32("an int32", &out) == B_NAME_NOT_FOUND);
29 	CPPUNIT_ASSERT(out == 0);
30 	CPPUNIT_ASSERT(msg.FindInt32("an int32") == 0);
31 	CPPUNIT_ASSERT(!msg.HasInt32("an int32"));
32 	const void* ptr = &out;
33 	ssize_t size;
34 	CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, &ptr, &size) ==
35 				   B_NAME_NOT_FOUND);
36 	CPPUNIT_ASSERT(ptr == NULL);
37 }
38 //------------------------------------------------------------------------------
39 void TMessageInt32ItemTest::MessageInt32ItemTest2()
40 {
41 	BMessage msg;
42 	int32 in = 1234;
43 	int32 out = 0;
44 	CPPUNIT_ASSERT(msg.AddInt32("an int32", in) == B_OK);
45 	CPPUNIT_ASSERT(msg.HasInt32("an int32"));
46 	CPPUNIT_ASSERT(msg.FindInt32("an int32") == in);
47 	CPPUNIT_ASSERT(msg.FindInt32("an int32", &out) == B_OK);
48 	CPPUNIT_ASSERT(out == in);
49 	int32* pout;
50 	ssize_t size;
51 	CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, (const void**)&pout,
52 				   &size) == B_OK);
53 	CPPUNIT_ASSERT(*pout == in);
54 	CPPUNIT_ASSERT(size == sizeof (int32));
55 }
56 //------------------------------------------------------------------------------
57 void TMessageInt32ItemTest::MessageInt32ItemTest3()
58 {
59 	BMessage msg;
60 	int32 in = 1234;
61 	int32 in2 = 7890;
62 	int32 out = 0;
63 	CPPUNIT_ASSERT(msg.AddInt32("an int32", in) == B_OK);
64 	CPPUNIT_ASSERT(msg.ReplaceInt32("an int32", in2) == B_OK);
65 	CPPUNIT_ASSERT(msg.HasInt32("an int32"));
66 	CPPUNIT_ASSERT(msg.FindInt32("an int32") == in2);
67 	CPPUNIT_ASSERT(msg.FindInt32("an int32", &out) == B_OK);
68 	CPPUNIT_ASSERT(out == in2);
69 	out = 0;
70 	int32* pout;
71 	ssize_t size;
72 	CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, (const void**)&pout,
73 				   &size) == B_OK);
74 	CPPUNIT_ASSERT(*pout == in2);
75 	CPPUNIT_ASSERT(size == sizeof (int32));
76 }
77 //------------------------------------------------------------------------------
78 void TMessageInt32ItemTest::MessageInt32ItemTest4()
79 {
80 	BMessage msg;
81 	int32 out = 0;
82 	CPPUNIT_ASSERT(msg.FindInt32("an int32", 1, &out) == B_NAME_NOT_FOUND);
83 	CPPUNIT_ASSERT(out == 0);
84 	CPPUNIT_ASSERT(msg.FindInt32("an int32", 1) == 0);
85 	CPPUNIT_ASSERT(!msg.HasInt32("an int32", 1));
86 	const void* ptr = &out;
87 	ssize_t size;
88 	CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, 1, &ptr, &size) ==
89 				   B_NAME_NOT_FOUND);
90 	CPPUNIT_ASSERT(ptr == NULL);
91 }
92 //------------------------------------------------------------------------------
93 void TMessageInt32ItemTest::MessageInt32ItemTest5()
94 {
95 	BMessage msg;
96 	int32 in[] = { 123, 456, 789 };
97 	int32 out = 0;
98 	int32* pout;
99 	ssize_t size;
100 
101 	for (int32 i = 0; i < sizeof (in) / sizeof (int32); ++i)
102 	{
103 		CPPUNIT_ASSERT(msg.AddInt32("an int32", in[i]) == B_OK);
104 	}
105 
106 	for (int32 i = 0; i < sizeof (in) / sizeof (int32); ++i)
107 	{
108 		CPPUNIT_ASSERT(msg.HasInt32("an int32", i));
109 		CPPUNIT_ASSERT(msg.FindInt32("an int32", i) == in[i]);
110 		CPPUNIT_ASSERT(msg.FindInt32("an int32", i, &out) == B_OK);
111 		CPPUNIT_ASSERT(out == in[i]);
112 		CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, i,
113 					   (const void**)&pout, &size) == B_OK);
114 		CPPUNIT_ASSERT(*pout == in[i]);
115 		CPPUNIT_ASSERT(size == sizeof (int32));
116 	}
117 }
118 //------------------------------------------------------------------------------
119 void TMessageInt32ItemTest::MessageInt32ItemTest6()
120 {
121 	BMessage msg;
122 	int32 in[] = { 123, 456, 789 };
123 	int32 in2 = 654;
124 	int32 out = 0;
125 	const int rIndex = 2;
126 
127 	for (int32 i = 0; i < sizeof (in) / sizeof (int32); ++i)
128 	{
129 		CPPUNIT_ASSERT(msg.AddInt32("an int32", in[i]) == B_OK);
130 	}
131 
132 	CPPUNIT_ASSERT(msg.ReplaceInt32("an int32", rIndex, in2) == B_OK);
133 	CPPUNIT_ASSERT(msg.HasInt32("an int32", rIndex));
134 	CPPUNIT_ASSERT(msg.FindInt32("an int32", rIndex) == in2);
135 	CPPUNIT_ASSERT(msg.FindInt32("an int32", rIndex, &out) == B_OK);
136 	CPPUNIT_ASSERT(out == in2);
137 	out = 0;
138 	int32* pout;
139 	ssize_t size;
140 	CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, rIndex,
141 				   (const void**)&pout, &size) == B_OK);
142 	CPPUNIT_ASSERT(*pout == in2);
143 	CPPUNIT_ASSERT(size == sizeof (int32));
144 }
145 //------------------------------------------------------------------------------
146 void TMessageInt32ItemTest::MessageInt32ItemTest7()
147 {
148 	BMessage msg;
149 	int32 in = 1234;
150 	int32 out = 0;
151 	CPPUNIT_ASSERT(msg.AddData("an int32", B_INT32_TYPE, (const void*)&in,
152 							   sizeof (int32)) == B_OK);
153 	CPPUNIT_ASSERT(msg.HasInt32("an int32"));
154 	CPPUNIT_ASSERT(msg.FindInt32("an int32") == in);
155 	CPPUNIT_ASSERT(msg.FindInt32("an int32", &out) == B_OK);
156 	CPPUNIT_ASSERT(out == in);
157 	int32* pout;
158 	ssize_t size;
159 	CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, (const void**)&pout,
160 				   &size) == B_OK);
161 	CPPUNIT_ASSERT(*pout == in);
162 	CPPUNIT_ASSERT(size == sizeof (int32));
163 }
164 //------------------------------------------------------------------------------
165 void TMessageInt32ItemTest::MessageInt32ItemTest8()
166 {
167 	BMessage msg;
168 	int32 in[] = { 123, 456, 789 };
169 	int32 out = 0;
170 	int32* pout;
171 	ssize_t size;
172 
173 	for (int32 i = 0; i < sizeof (in) / sizeof (int32); ++i)
174 	{
175 		CPPUNIT_ASSERT(msg.AddData("an int32", B_INT32_TYPE,
176 					   (const void*)&in[i], sizeof (int32)) == B_OK);
177 	}
178 
179 	for (int32 i = 0; i < sizeof (in) / sizeof (int32); ++i)
180 	{
181 		CPPUNIT_ASSERT(msg.HasInt32("an int32", i));
182 		CPPUNIT_ASSERT(msg.FindInt32("an int32", i) == in[i]);
183 		CPPUNIT_ASSERT(msg.FindInt32("an int32", i, &out) == B_OK);
184 		CPPUNIT_ASSERT(out == in[i]);
185 		CPPUNIT_ASSERT(msg.FindData("an int32", B_INT32_TYPE, i,
186 					   (const void**)&pout, &size) == B_OK);
187 		CPPUNIT_ASSERT(*pout == in[i]);
188 		CPPUNIT_ASSERT(size == sizeof (int32));
189 	}
190 }
191 //------------------------------------------------------------------------------
192 TestSuite* TMessageInt32ItemTest::Suite()
193 {
194 	TestSuite* suite = new TestSuite("BMessage::Add/Find/Replace/HasRect()");
195 
196 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest1);
197 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest2);
198 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest3);
199 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest4);
200 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest5);
201 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest6);
202 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest7);
203 	ADD_TEST4(BMessage, suite, TMessageInt32ItemTest, MessageInt32ItemTest8);
204 
205 	return suite;
206 }
207 //------------------------------------------------------------------------------
208 #endif
209 
210 /*
211  * $Log $
212  *
213  * $Id  $
214  *
215  */
216 
217