PageRenderTime 45ms CodeModel.GetById 2ms app.highlight 39ms RepoModel.GetById 1ms 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
  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
 30#include "common/linux/linux_libc_support.h"
 31#include "testing/gtest/include/gtest/gtest.h"
 32
 33namespace {
 34typedef testing::Test LinuxLibcSupportTest;
 35}
 36
 37TEST(LinuxLibcSupportTest, strlen) {
 38  static const char* test_data[] = { "", "a", "aa", "aaa", "aabc", NULL };
 39  for (unsigned i = 0; ; ++i) {
 40    if (!test_data[i])
 41      break;
 42    ASSERT_EQ(strlen(test_data[i]), my_strlen(test_data[i]));
 43  }
 44}
 45
 46TEST(LinuxLibcSupportTest, strcmp) {
 47  static const char* test_data[] = {
 48    "", "",
 49    "a", "",
 50    "", "a",
 51    "a", "b",
 52    "a", "a",
 53    "ab", "aa",
 54    "abc", "ab",
 55    "abc", "abc",
 56    NULL,
 57  };
 58
 59  for (unsigned i = 0; ; ++i) {
 60    if (!test_data[i*2])
 61      break;
 62    int libc_result = strcmp(test_data[i*2], test_data[i*2 + 1]);
 63    if (libc_result > 1)
 64      libc_result = 1;
 65    else if (libc_result < -1)
 66      libc_result = -1;
 67    ASSERT_EQ(my_strcmp(test_data[i*2], test_data[i*2 + 1]), libc_result);
 68  }
 69}
 70
 71TEST(LinuxLibcSupportTest, strtoui) {
 72  int result;
 73
 74  ASSERT_FALSE(my_strtoui(&result, ""));
 75  ASSERT_FALSE(my_strtoui(&result, "-1"));
 76  ASSERT_FALSE(my_strtoui(&result, "-"));
 77  ASSERT_FALSE(my_strtoui(&result, "a"));
 78  ASSERT_FALSE(my_strtoui(&result, "23472893472938472987987398472398"));
 79
 80  ASSERT_TRUE(my_strtoui(&result, "0"));
 81  ASSERT_EQ(result, 0);
 82  ASSERT_TRUE(my_strtoui(&result, "1"));
 83  ASSERT_EQ(result, 1);
 84  ASSERT_TRUE(my_strtoui(&result, "12"));
 85  ASSERT_EQ(result, 12);
 86  ASSERT_TRUE(my_strtoui(&result, "123"));
 87  ASSERT_EQ(result, 123);
 88  ASSERT_TRUE(my_strtoui(&result, "0123"));
 89  ASSERT_EQ(result, 123);
 90}
 91
 92TEST(LinuxLibcSupportTest, int_len) {
 93  ASSERT_EQ(my_int_len(0), 1);
 94  ASSERT_EQ(my_int_len(2), 1);
 95  ASSERT_EQ(my_int_len(5), 1);
 96  ASSERT_EQ(my_int_len(9), 1);
 97  ASSERT_EQ(my_int_len(10), 2);
 98  ASSERT_EQ(my_int_len(99), 2);
 99  ASSERT_EQ(my_int_len(100), 3);
100  ASSERT_EQ(my_int_len(101), 3);
101  ASSERT_EQ(my_int_len(1000), 4);
102}
103
104TEST(LinuxLibcSupportTest, itos) {
105  char buf[10];
106
107  my_itos(buf, 0, 1);
108  ASSERT_EQ(0, memcmp(buf, "0", 1));
109
110  my_itos(buf, 1, 1);
111  ASSERT_EQ(0, memcmp(buf, "1", 1));
112
113  my_itos(buf, 10, 2);
114  ASSERT_EQ(0, memcmp(buf, "10", 2));
115
116  my_itos(buf, 63, 2);
117  ASSERT_EQ(0, memcmp(buf, "63", 2));
118
119  my_itos(buf, 101, 3);
120  ASSERT_EQ(0, memcmp(buf, "101", 2));
121}
122
123TEST(LinuxLibcSupportTest, strchr) {
124  ASSERT_EQ(NULL, my_strchr("abc", 'd'));
125  ASSERT_EQ(NULL, my_strchr("", 'd'));
126  ASSERT_EQ(NULL, my_strchr("efghi", 'd'));
127
128  ASSERT_TRUE(my_strchr("a", 'a'));
129  ASSERT_TRUE(my_strchr("abc", 'a'));
130  ASSERT_TRUE(my_strchr("bcda", 'a'));
131  ASSERT_TRUE(my_strchr("sdfasdf", 'a'));
132}
133
134TEST(LinuxLibcSupportTest, read_hex_ptr) {
135  uintptr_t result;
136  const char* last;
137
138  last = my_read_hex_ptr(&result, "");
139  ASSERT_EQ(result, 0);
140  ASSERT_EQ(*last, 0);
141
142  last = my_read_hex_ptr(&result, "0");
143  ASSERT_EQ(result, 0);
144  ASSERT_EQ(*last, 0);
145
146  last = my_read_hex_ptr(&result, "0123");
147  ASSERT_EQ(result, 0x123);
148  ASSERT_EQ(*last, 0);
149
150  last = my_read_hex_ptr(&result, "0123a");
151  ASSERT_EQ(result, 0x123a);
152  ASSERT_EQ(*last, 0);
153
154  last = my_read_hex_ptr(&result, "0123a-");
155  ASSERT_EQ(result, 0x123a);
156  ASSERT_EQ(*last, '-');
157}