Size: 3567 bytes.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// cs/q/search_big_array/search_big_array_test.cc
extern "C" {
#include "cs/q/search_big_array/search_big_array.h"
}

#include <stdio.h>

#include <iostream>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace {
using ::testing::AtLeast;
using ::testing::Eq;
using ::testing::FloatEq;
using ::testing::Gt;
using ::testing::IsFalse;
using ::testing::IsTrue;
using ::testing::Matcher;
using ::testing::Not;
}  // namespace

namespace {  // use_usings
int* MakeArray(int length) {
  EXPECT_THAT(length, Gt(0));
  int* array = (int*)malloc(sizeof(int) * length);
  EXPECT_THAT(array, Not(Eq((int*)NULL)));
  return array;
}

TEST(InitArrayReader, Success) {
  int length = 3;
  int* array = MakeArray(length);
  InitArrayReader(array, length);
}

TEST(ArrayReaderGet, Success) {
  int length = 3;
  int* array = MakeArray(length);
  array[0] = -1;
  array[1] = 0;
  array[2] = 1;
  InitArrayReader(array, length);

  EXPECT_THAT(ArrayReaderGet(0), -1);
  EXPECT_THAT(ArrayReaderGet(1), 0);
  EXPECT_THAT(ArrayReaderGet(2), 1);
}

/*
Example 1:

```
Input: array = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
```
*/
TEST(Search, LeetcodeExample1) {
  int length = 6;
  int* array = MakeArray(length);
  array[0] = -1;
  array[1] = 0;
  array[2] = 3;
  array[3] = 5;
  array[4] = 9;
  array[5] = 12;
  InitArrayReader(array, length);

  EXPECT_THAT(Search(9), Eq(4));
}

/*
Example 2:

```
Input: array = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
```
*/
TEST(Search, LeetcodeExample2) {
  int length = 6;
  int* array = MakeArray(length);
  array[0] = -1;
  array[1] = 0;
  array[2] = 3;
  array[3] = 5;
  array[4] = 9;
  array[5] = 12;
  InitArrayReader(array, length);

  EXPECT_THAT(Search(2), Eq(-1));
}

// From ChatGPT:

// Helper to initialize array from std::vector
void InitArrayReaderFromVector(
    const std::vector<int>& vec) {
  InitArrayReader(const_cast<int*>(vec.data()),
                  static_cast<int>(vec.size()));
}

TEST(SearchInUnknownSizeArrayTest, TargetDoesNotExist) {
  std::vector<int> nums = {-1, 0, 3, 5, 9, 12};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(2), -1);
}

TEST(SearchInUnknownSizeArrayTest, TargetIsFirstElement) {
  std::vector<int> nums = {-5, 1, 4, 7, 10};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(-5), 0);
}

TEST(SearchInUnknownSizeArrayTest, TargetIsLastElement) {
  std::vector<int> nums = {-5, 1, 4, 7, 10};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(10), 4);
}

TEST(SearchInUnknownSizeArrayTest, SingleElementFound) {
  std::vector<int> nums = {42};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(42), 0);
}

TEST(SearchInUnknownSizeArrayTest, SingleElementNotFound) {
  std::vector<int> nums = {42};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(99), -1);
}

TEST(SearchInUnknownSizeArrayTest, EmptyArray) {
  std::vector<int> nums = {};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(5), -1);
}

TEST(SearchInUnknownSizeArrayTest, NegativeNumbers) {
  std::vector<int> nums = {-9999, -5000, -100, -1};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(-5000), 1);
}

TEST(SearchInUnknownSizeArrayTest, LargePositiveNumbers) {
  std::vector<int> nums = {5000, 7000, 9000, 9999};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(Search(9999), 3);
}

TEST(SearchInUnknownSizeArrayTest,
     ArrayReaderOutOfBoundsReturnsINTMAX) {
  std::vector<int> nums = {1, 3, 5};
  InitArrayReaderFromVector(nums);
  EXPECT_EQ(ArrayReaderGet(10), INT_MAX);
}

}  // namespace
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.