leb128_unittest.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // leb_unittest.cc -- test read_signed_LEB_128 and read_unsigned_LEB_128
  2. // Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3. // Written by Cary Coutant <ccoutant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <sys/types.h>
  19. #include "int_encoding.h"
  20. #include "test.h"
  21. namespace gold_testsuite
  22. {
  23. using namespace gold;
  24. bool
  25. Leb128_test(Test_report*)
  26. {
  27. size_t len;
  28. // Unsigned tests.
  29. static unsigned char u1[] = { 0 }; // 0
  30. static unsigned char u2[] = { 1 }; // 1
  31. static unsigned char u3[] = { 126 }; // 126
  32. static unsigned char u4[] = { 127 }; // 127
  33. static unsigned char u5[] = { 0x80+0, 1 }; // 128
  34. static unsigned char u6[] = { 0x80+1, 1 }; // 129
  35. static unsigned char u7[] = { 0x80+57, 100 }; // 12857
  36. static unsigned char u8[] = { 0x80, 0x80, 0x80, 0x80,
  37. 0x80, 0x80, 0x80, 0x80,
  38. 0x80, 1}; // 1ULL << 63
  39. // Signed tests.
  40. static unsigned char s1[] = { 0 }; // 0
  41. static unsigned char s2[] = { 1 }; // 1
  42. static unsigned char s3[] = { 0x7e }; // -2
  43. static unsigned char s4[] = { 0x80+127, 0 }; // 127
  44. static unsigned char s5[] = { 0x80+1, 0x7f }; // -127
  45. static unsigned char s6[] = { 0x80+0, 1 }; // 128
  46. static unsigned char s7[] = { 0x80+0, 0x7f }; // -128
  47. static unsigned char s8[] = { 0x80+1, 1 }; // 129
  48. static unsigned char s9[] = { 0xff, 0x7e }; // -129
  49. CHECK(read_unsigned_LEB_128(u1, &len) == 0 && len == sizeof(u1));
  50. CHECK(read_unsigned_LEB_128(u2, &len) == 1 && len == sizeof(u2));
  51. CHECK(read_unsigned_LEB_128(u3, &len) == 126 && len == sizeof(u3));
  52. CHECK(read_unsigned_LEB_128(u4, &len) == 127 && len == sizeof(u4));
  53. CHECK(read_unsigned_LEB_128(u5, &len) == 128 && len == sizeof(u5));
  54. CHECK(read_unsigned_LEB_128(u6, &len) == 129 && len == sizeof(u6));
  55. CHECK(read_unsigned_LEB_128(u7, &len) == 12857 && len == sizeof(u7));
  56. CHECK(read_unsigned_LEB_128(u8, &len) == (1ULL << 63) && len == sizeof(u8));
  57. CHECK(read_signed_LEB_128(s1, &len) == 0 && len == sizeof(s1));
  58. CHECK(read_signed_LEB_128(s2, &len) == 1 && len == sizeof(s2));
  59. CHECK(read_signed_LEB_128(s3, &len) == -2 && len == sizeof(s3));
  60. CHECK(read_signed_LEB_128(s4, &len) == 127 && len == sizeof(s4));
  61. CHECK(read_signed_LEB_128(s5, &len) == -127 && len == sizeof(s5));
  62. CHECK(read_signed_LEB_128(s6, &len) == 128 && len == sizeof(s6));
  63. CHECK(read_signed_LEB_128(s7, &len) == -128 && len == sizeof(s7));
  64. CHECK(read_signed_LEB_128(s8, &len) == 129 && len == sizeof(s8));
  65. CHECK(read_signed_LEB_128(s9, &len) == -129 && len == sizeof(s9));
  66. return true;
  67. }
  68. Register_test leb128_register("LEB128", Leb128_test);
  69. } // End namespace gold_testsuite.