PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  29. #include "../bitpack.h"
  30. #include "../test/lltut.h"
  31. namespace tut
  32. {
  33. struct bit_pack
  34. {
  35. };
  36. typedef test_group<bit_pack> bit_pack_t;
  37. typedef bit_pack_t::object bit_pack_object_t;
  38. tut::bit_pack_t tut_bit_pack("LLBitPack");
  39. // pack -> unpack
  40. template<> template<>
  41. void bit_pack_object_t::test<1>()
  42. {
  43. U8 packbuffer[255];
  44. U8 unpackbuffer[255];
  45. int pack_bufsize = 0;
  46. int unpack_bufsize = 0;
  47. LLBitPack bitpack(packbuffer, 255);
  48. char str[] = "SecondLife is a 3D virtual world";
  49. int len = sizeof(str);
  50. pack_bufsize = bitpack.bitPack((U8*) str, len*8);
  51. pack_bufsize = bitpack.flushBitPack();
  52. LLBitPack bitunpack(packbuffer, pack_bufsize*8);
  53. unpack_bufsize = bitunpack.bitUnpack(unpackbuffer, len*8);
  54. ensure("bitPack: unpack size should be same as string size prior to pack", len == unpack_bufsize);
  55. ensure_memory_matches("str->bitPack->bitUnpack should be equal to string", str, len, unpackbuffer, unpack_bufsize);
  56. }
  57. // pack large, unpack in individual bytes
  58. template<> template<>
  59. void bit_pack_object_t::test<2>()
  60. {
  61. U8 packbuffer[255];
  62. U8 unpackbuffer[255];
  63. int pack_bufsize = 0;
  64. int unpack_bufsize = 0;
  65. LLBitPack bitpack(packbuffer, 255);
  66. char str[] = "SecondLife";
  67. int len = sizeof(str);
  68. pack_bufsize = bitpack.bitPack((U8*) str, len*8);
  69. pack_bufsize = bitpack.flushBitPack();
  70. LLBitPack bitunpack(packbuffer, pack_bufsize*8);
  71. unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
  72. ensure("bitPack: individual unpack: 0", unpackbuffer[0] == (U8) str[0]);
  73. unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
  74. ensure("bitPack: individual unpack: 1", unpackbuffer[0] == (U8) str[1]);
  75. unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
  76. ensure("bitPack: individual unpack: 2", unpackbuffer[0] == (U8) str[2]);
  77. unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
  78. ensure("bitPack: individual unpack: 3", unpackbuffer[0] == (U8) str[3]);
  79. unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
  80. ensure("bitPack: individual unpack: 4", unpackbuffer[0] == (U8) str[4]);
  81. unpack_bufsize = bitunpack.bitUnpack(&unpackbuffer[0], 8);
  82. ensure("bitPack: individual unpack: 5", unpackbuffer[0] == (U8) str[5]);
  83. unpack_bufsize = bitunpack.bitUnpack(unpackbuffer, 8*4); // Life
  84. ensure_memory_matches("bitPack: 4 bytes unpack:", unpackbuffer, 4, str+6, 4);
  85. }
  86. // U32 packing
  87. template<> template<>
  88. void bit_pack_object_t::test<3>()
  89. {
  90. U8 packbuffer[255];
  91. int pack_bufsize = 0;
  92. LLBitPack bitpack(packbuffer, 255);
  93. U32 num = 0x41fab67a;
  94. pack_bufsize = bitpack.bitPack((U8*)&num, 8*sizeof(U32));
  95. pack_bufsize = bitpack.flushBitPack();
  96. LLBitPack bitunpack(packbuffer, pack_bufsize*8);
  97. U32 res = 0;
  98. // since packing and unpacking is done on same machine in the unit test run,
  99. // endianness should not matter
  100. bitunpack.bitUnpack((U8*) &res, sizeof(res)*8);
  101. ensure("U32->bitPack->bitUnpack->U32 should be equal", num == res);
  102. }
  103. }