PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/breakpad/common/linux/linux_libc_support_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 157 lines | 106 code | 23 blank | 28 comment | 7 complexity | 8f8e9c81bd6591a0aa510b2220754493 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Copyright (c) 2009, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #include "common/linux/linux_libc_support.h"
  30. #include "testing/gtest/include/gtest/gtest.h"
  31. namespace {
  32. typedef testing::Test LinuxLibcSupportTest;
  33. }
  34. TEST(LinuxLibcSupportTest, strlen) {
  35. static const char* test_data[] = { "", "a", "aa", "aaa", "aabc", NULL };
  36. for (unsigned i = 0; ; ++i) {
  37. if (!test_data[i])
  38. break;
  39. ASSERT_EQ(strlen(test_data[i]), my_strlen(test_data[i]));
  40. }
  41. }
  42. TEST(LinuxLibcSupportTest, strcmp) {
  43. static const char* test_data[] = {
  44. "", "",
  45. "a", "",
  46. "", "a",
  47. "a", "b",
  48. "a", "a",
  49. "ab", "aa",
  50. "abc", "ab",
  51. "abc", "abc",
  52. NULL,
  53. };
  54. for (unsigned i = 0; ; ++i) {
  55. if (!test_data[i*2])
  56. break;
  57. int libc_result = strcmp(test_data[i*2], test_data[i*2 + 1]);
  58. if (libc_result > 1)
  59. libc_result = 1;
  60. else if (libc_result < -1)
  61. libc_result = -1;
  62. ASSERT_EQ(my_strcmp(test_data[i*2], test_data[i*2 + 1]), libc_result);
  63. }
  64. }
  65. TEST(LinuxLibcSupportTest, strtoui) {
  66. int result;
  67. ASSERT_FALSE(my_strtoui(&result, ""));
  68. ASSERT_FALSE(my_strtoui(&result, "-1"));
  69. ASSERT_FALSE(my_strtoui(&result, "-"));
  70. ASSERT_FALSE(my_strtoui(&result, "a"));
  71. ASSERT_FALSE(my_strtoui(&result, "23472893472938472987987398472398"));
  72. ASSERT_TRUE(my_strtoui(&result, "0"));
  73. ASSERT_EQ(result, 0);
  74. ASSERT_TRUE(my_strtoui(&result, "1"));
  75. ASSERT_EQ(result, 1);
  76. ASSERT_TRUE(my_strtoui(&result, "12"));
  77. ASSERT_EQ(result, 12);
  78. ASSERT_TRUE(my_strtoui(&result, "123"));
  79. ASSERT_EQ(result, 123);
  80. ASSERT_TRUE(my_strtoui(&result, "0123"));
  81. ASSERT_EQ(result, 123);
  82. }
  83. TEST(LinuxLibcSupportTest, int_len) {
  84. ASSERT_EQ(my_int_len(0), 1);
  85. ASSERT_EQ(my_int_len(2), 1);
  86. ASSERT_EQ(my_int_len(5), 1);
  87. ASSERT_EQ(my_int_len(9), 1);
  88. ASSERT_EQ(my_int_len(10), 2);
  89. ASSERT_EQ(my_int_len(99), 2);
  90. ASSERT_EQ(my_int_len(100), 3);
  91. ASSERT_EQ(my_int_len(101), 3);
  92. ASSERT_EQ(my_int_len(1000), 4);
  93. }
  94. TEST(LinuxLibcSupportTest, itos) {
  95. char buf[10];
  96. my_itos(buf, 0, 1);
  97. ASSERT_EQ(0, memcmp(buf, "0", 1));
  98. my_itos(buf, 1, 1);
  99. ASSERT_EQ(0, memcmp(buf, "1", 1));
  100. my_itos(buf, 10, 2);
  101. ASSERT_EQ(0, memcmp(buf, "10", 2));
  102. my_itos(buf, 63, 2);
  103. ASSERT_EQ(0, memcmp(buf, "63", 2));
  104. my_itos(buf, 101, 3);
  105. ASSERT_EQ(0, memcmp(buf, "101", 2));
  106. }
  107. TEST(LinuxLibcSupportTest, strchr) {
  108. ASSERT_EQ(NULL, my_strchr("abc", 'd'));
  109. ASSERT_EQ(NULL, my_strchr("", 'd'));
  110. ASSERT_EQ(NULL, my_strchr("efghi", 'd'));
  111. ASSERT_TRUE(my_strchr("a", 'a'));
  112. ASSERT_TRUE(my_strchr("abc", 'a'));
  113. ASSERT_TRUE(my_strchr("bcda", 'a'));
  114. ASSERT_TRUE(my_strchr("sdfasdf", 'a'));
  115. }
  116. TEST(LinuxLibcSupportTest, read_hex_ptr) {
  117. uintptr_t result;
  118. const char* last;
  119. last = my_read_hex_ptr(&result, "");
  120. ASSERT_EQ(result, 0);
  121. ASSERT_EQ(*last, 0);
  122. last = my_read_hex_ptr(&result, "0");
  123. ASSERT_EQ(result, 0);
  124. ASSERT_EQ(*last, 0);
  125. last = my_read_hex_ptr(&result, "0123");
  126. ASSERT_EQ(result, 0x123);
  127. ASSERT_EQ(*last, 0);
  128. last = my_read_hex_ptr(&result, "0123a");
  129. ASSERT_EQ(result, 0x123a);
  130. ASSERT_EQ(*last, 0);
  131. last = my_read_hex_ptr(&result, "0123a-");
  132. ASSERT_EQ(result, 0x123a);
  133. ASSERT_EQ(*last, '-');
  134. }