Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
string-search-test.cc
Go to the documentation of this file.
1 // Copyright (c) 2012 Cloudera, Inc. All rights reserved.
2 
3 #include <string>
4 #include <gtest/gtest.h>
5 
7 
8 #include "common/names.h"
9 
10 namespace impala {
11 
12 // Test string search for haystack/needle, up to len for each.
13 // If the length is -1, use the full string length
14 // haystack/needle are null terminated
15 void TestSearch(const char* haystack_orig, const char* needle_orig,
16  int haystack_len = -1, int needle_len = -1) {
17 
18  string haystack_copy(haystack_orig);
19  string needle_copy(needle_orig);
20  const char* haystack = haystack_copy.c_str();
21  const char* needle = needle_copy.c_str();
22 
23  string haystack_buffer, needle_buffer;
24 
25  const char* haystack_null_terminated = haystack;
26  const char* needle_null_terminated = needle;
27 
28  // Make null terminated versions (for libc).
29  if (haystack_len != -1) {
30  haystack_buffer = string(haystack, haystack_len);
31  haystack_null_terminated = haystack_buffer.c_str();
32  } else {
33  haystack_len = strlen(haystack);
34  }
35 
36  if (needle_len != -1) {
37  needle_buffer = string(needle, needle_len);
38  needle_null_terminated = needle_buffer.c_str();
39  } else {
40  needle_len = strlen(needle);
41  }
42 
43  // Call libc for correct result
44  const char* libc_result = strstr(haystack_null_terminated, needle_null_terminated);
45  int libc_offset = (libc_result == NULL) ? -1 : libc_result - haystack_null_terminated;
46 
47  StringValue haystack_str_val(const_cast<char*>(haystack), haystack_len);
48 
49  // Call our strstr with null terminated needle
50  StringSearchSSE needle1(needle_null_terminated);
51  int null_offset = needle1.Search(haystack_str_val);
52  EXPECT_EQ(null_offset, libc_offset);
53 
54  // Call our strstr with non-null terminated needle
55  StringValue needle_str_val(const_cast<char*>(needle), needle_len);
56  StringSearchSSE needle2(&needle_str_val);
57  int not_null_offset = needle2.Search(haystack_str_val);
58  EXPECT_EQ(not_null_offset, libc_offset);
59 
60  // Ensure haystack/needle are unmodified
61  EXPECT_EQ(strlen(needle_null_terminated), needle_len);
62  EXPECT_EQ(strlen(haystack_null_terminated), haystack_len);
63  EXPECT_EQ(strcmp(haystack, haystack_orig), 0);
64  EXPECT_EQ(strcmp(needle, needle_orig), 0);
65 }
66 
67 
68 TEST(StringSearchTest, Basic) {
69  // Basic Tests
70  TestSearch("abcd", "a");
71  TestSearch("abcd", "ab");
72  TestSearch("abcd", "c");
73  TestSearch("abcd", "cd");
74  TestSearch("", "");
75  TestSearch("abc", "");
76  TestSearch("", "a");
77 
78  // Test single chars
79  TestSearch("a", "a");
80  TestSearch("a", "b");
81  TestSearch("abc", "b");
82 
83  // Haystack is not full string
84  TestSearch("abcabcd", "abc", 5);
85  TestSearch("abcabcd", "abcd", 5);
86  TestSearch("abcabcd", "abcd", 0);
87 
88  // Haystack and needle not full len
89  TestSearch("abcde", "abaaaaaa", 4, 2);
90  TestSearch("abcde", "abaaaaaa", 4, 3);
91  TestSearch("abcdabaaaaa", "abaaaaaa", 4, 3);
92  TestSearch("abcdabaaaaa", "abaaaaaa", 4, 0);
93  TestSearch("abcdabaaaaa", "abaaaaaa", 0, 0);
94 
95  // Test last bit, this is the interesting edge case
96  TestSearch("abcde", "e");
97  TestSearch("abcde", "de");
98  TestSearch("abcdede", "cde");
99  TestSearch("abcdacde", "cde");
100 }
101 
102 }
103 
104 int main(int argc, char **argv) {
105  ::testing::InitGoogleTest(&argc, argv);
106  return RUN_ALL_TESTS();
107 }
108 
int main(int argc, char **argv)
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
void TestSearch(const char *haystack_orig, const char *needle_orig, int haystack_len=-1, int needle_len=-1)
int Search(const StringValue &haystack) const