aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/llbitpack_tut.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/test/llbitpack_tut.cpp')
-rw-r--r--linden/indra/test/llbitpack_tut.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/linden/indra/test/llbitpack_tut.cpp b/linden/indra/test/llbitpack_tut.cpp
new file mode 100644
index 0000000..1040a96
--- /dev/null
+++ b/linden/indra/test/llbitpack_tut.cpp
@@ -0,0 +1,119 @@
1/**
2 * @file llbitpack_tut.cpp
3 * @author Adroit
4 * @date February 2007
5 * @brief llstreamtools test cases.
6 *
7 * Copyright (c) 2007-2007, Linden Research, Inc.
8 *
9 * The source code in this file ("Source Code") is provided by Linden Lab
10 * to you under the terms of the GNU General Public License, version 2.0
11 * ("GPL"), unless you have obtained a separate licensing agreement
12 * ("Other License"), formally executed by you and Linden Lab. Terms of
13 * the GPL can be found in doc/GPL-license.txt in this distribution, or
14 * online at http://secondlife.com/developers/opensource/gplv2
15 *
16 * There are special exceptions to the terms and conditions of the GPL as
17 * it is applied to this Source Code. View the full text of the exception
18 * in the file doc/FLOSS-exception.txt in this software distribution, or
19 * online at http://secondlife.com/developers/opensource/flossexception
20 *
21 * By copying, modifying or distributing this software, you acknowledge
22 * that you have read and understood your obligations described above,
23 * and agree to abide by those obligations.
24 *
25 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
26 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
27 * COMPLETENESS OR PERFORMANCE.
28 */
29
30#include <tut/tut.h>
31#include "linden_common.h"
32#include "bitpack.h"
33#include "lltut.h"
34
35
36namespace tut
37{
38 struct bit_pack
39 {
40 };
41 typedef test_group<bit_pack> bit_pack_t;
42 typedef bit_pack_t::object bit_pack_object_t;
43 tut::bit_pack_t tut_bit_pack("bitpack");
44
45 // pack -> unpack
46 template<> template<>
47 void bit_pack_object_t::test<1>()
48 {
49 U8 packbuffer[255];
50 U8 unpackbuffer[255];
51 int pack_bufsize = 0;
52 int unpack_bufsize = 0;
53
54 LLBitPack bitpack(packbuffer, 255);
55
56 char str[] = "SecondLife is a 3D virtual world";
57 int len = sizeof(str);
58 pack_bufsize = bitpack.bitPack((U8*) str, len*8);
59 pack_bufsize = bitpack.flushBitPack();
60
61 LLBitPack bitunpack(packbuffer, pack_bufsize*8);
62 unpack_bufsize = bitunpack.bitUnpack(unpackbuffer, len*8);
63 ensure("bitPack: unpack size should be same as string size prior to pack", len == unpack_bufsize);
64 ensure_memory_matches("str->bitPack->bitUnpack should be equal to string", str, len, unpackbuffer, unpack_bufsize);
65 }
66
67 // pack large, unpack in individual bytes
68 template<> template<>
69 void bit_pack_object_t::test<2>()
70 {
71 U8 packbuffer[255];
72 U8 unpackbuffer[255];
73 int pack_bufsize = 0;
74 int unpack_bufsize = 0;
75
76 LLBitPack bitpack(packbuffer, 255);
77
78 char str[] = "SecondLife";
79 int len = sizeof(str);
80 pack_bufsize = bitpack.bitPack((U8*) str, len*8);
81 pack_bufsize = bitpack.flushBitPack();
82
83 LLBitPack bitunpack(packbuffer, pack_bufsize*8);
84 unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
85 ensure("bitPack: individual unpack: 0", unpackbuffer[0] == (U8) str[0]);
86 unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
87 ensure("bitPack: individual unpack: 1", unpackbuffer[0] == (U8) str[1]);
88 unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
89 ensure("bitPack: individual unpack: 2", unpackbuffer[0] == (U8) str[2]);
90 unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
91 ensure("bitPack: individual unpack: 3", unpackbuffer[0] == (U8) str[3]);
92 unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
93 ensure("bitPack: individual unpack: 4", unpackbuffer[0] == (U8) str[4]);
94 unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
95 ensure("bitPack: individual unpack: 5", unpackbuffer[0] == (U8) str[5]);
96 unpack_bufsize = bitunpack.bitUnpack(unpackbuffer, 8*4); // Life
97 ensure_memory_matches("bitPack: 4 bytes unpack:", unpackbuffer, 4, str+6, 4);
98 }
99
100 // U32 packing
101 template<> template<>
102 void bit_pack_object_t::test<3>()
103 {
104 U8 packbuffer[255];
105 int pack_bufsize = 0;
106
107 LLBitPack bitpack(packbuffer, 255);
108 U32 num = 0x41fab67a;
109 pack_bufsize = bitpack.bitPack((U8*)&num, 8*sizeof(U32));
110 pack_bufsize = bitpack.flushBitPack();
111
112 LLBitPack bitunpack(packbuffer, pack_bufsize*8);
113 U32 res = 0;
114 // since packing and unpacking is done on same machine in the unit test run,
115 // endianness should not matter
116 bitunpack.bitUnpack((U8*) &res, sizeof(res)*8);
117 ensure("U32->bitPack->bitUnpack->U32 should be equal", num == res);
118 }
119}