PageRenderTime 31ms CodeModel.GetById 10ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llcommon/tests/bitpack_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 119 lines | 71 code | 16 blank | 32 comment | 8 complexity | e5280fe289c08fb71e0693947803342c MD5 | raw file
  1/**
  2 * @file bitpack_test.cpp
  3 * @author Adroit
  4 * @date 2007-02
  5 * @brief llstreamtools test cases.
  6 *
  7 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28 
 29#include "linden_common.h"
 30
 31#include "../bitpack.h"
 32
 33#include "../test/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("LLBitPack");
 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}