summaryrefslogtreecommitdiff
path: root/tests/search_index_test.c
blob: 5a477ad115b1f78d21c61df0c2371a7e0ff8c8da (plain)
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
#include "domain/message.h"
#include "domain/search.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "base/allocator.h"

static int failures;

#define CHECK(value)                                                                               \
  do                                                                                               \
    {                                                                                              \
      if (!(value))                                                                                \
        {                                                                                          \
          fprintf(stderr, "CHECK failed at line %d: %s\n", __LINE__, #value);                      \
          failures++;                                                                              \
        }                                                                                          \
    }                                                                                              \
  while (0)

static tiny_message_t
make_message(const char *content, char **tags, size_t tag_count)
{
  tiny_message_t message;
  tiny_message_init(&message);
  message.content = (char *)content;
  message.metadata.tags = tags;
  message.metadata.tag_count = tag_count;
  return message;
}

static bool
candidate_contains(tiny_search_index_t *index, const char *query, size_t expected)
{
  size_t *candidates = NULL;
  size_t count = 0, position;
  bool found = false;
  if (!tiny_search_index_candidates(index, query, &candidates, &count))
    {
      return false;
    }
  for (position = 0; position < count; ++position)
    {
      if (candidates[position] == expected)
        {
          found = true;
          break;
        }
    }
  free(candidates);
  return found;
}

static void
test_collision_rehash_and_unicode(void)
{
  tiny_search_index_t *index = tiny_search_index_create();
  char contents[300][4];
  size_t position;
  char *tags[] = { "設計メモ" };
  tiny_message_t tagged = make_message("tagged content", tags, 1);
  tiny_message_t duplicate = make_message("aaaaaa", NULL, 0);
  size_t *candidates = NULL;
  size_t count = 0;

  CHECK(index != NULL);
  for (position = 0; index && position < 300; ++position)
    {
      tiny_message_t message;
      contents[position][0] = (char)('a' + (position / (26 * 26)) % 26);
      contents[position][1] = (char)('a' + (position / 26) % 26);
      contents[position][2] = (char)('a' + position % 26);
      contents[position][3] = '\0';
      message = make_message(contents[position], NULL, 0);
      CHECK(tiny_search_index_add(index, &message, position));
    }
  for (position = 0; index && position < 300; ++position)
    {
      CHECK(candidate_contains(index, contents[position], position));
    }

  CHECK(tiny_search_index_add(index, &tagged, 300));
  CHECK(candidate_contains(index, "設計", 300));
  CHECK(candidate_contains(index, "TAGGED", 300));
  CHECK(tiny_search_index_add(index, &duplicate, 301));
  CHECK(tiny_search_index_candidates(index, "aaa", &candidates, &count));
  {
    size_t duplicate_hits = 0;
    for (position = 0; position < count; ++position)
      {
        if (candidates[position] == 301)
          {
            duplicate_hits++;
          }
      }
    CHECK(duplicate_hits == 1);
  }
  free(candidates);
  candidates = NULL;
  CHECK(tiny_search_index_candidates(index, "a", &candidates, &count));
  CHECK(count == 302);
  free(candidates);
  tiny_search_index_free(index);
}

static void
test_transactional_allocation_failure(void)
{
  const tiny_message_t baseline = make_message("baseline-only", NULL, 0);
  char *tags[] = { "transaction-tag", "日本語追加" };
  const tiny_message_t addition
      = make_message("zyxwvutsrqponmlkjihgfedcba-transactional-new-token", tags, 2);
  size_t failure_index;
  bool reached_success = false;

  for (failure_index = 0; failure_index < 512; ++failure_index)
    {
      tiny_search_index_t *index;
      size_t baseline_allocations = tiny_allocator_outstanding();
      tiny_allocator_reset();
      index = tiny_search_index_create();
      CHECK(index != NULL);
      CHECK(tiny_search_index_add(index, &baseline, 0));

      tiny_allocator_fail_after(failure_index);
      if (tiny_search_index_add(index, &addition, 1))
        {
          tiny_allocator_reset();
          CHECK(candidate_contains(index, "transactional-new-token", 1));
          reached_success = true;
          tiny_search_index_free(index);
          CHECK(tiny_allocator_outstanding() == baseline_allocations);
          break;
        }

      tiny_allocator_reset();
      CHECK(candidate_contains(index, "baseline-only", 0));
      CHECK(!candidate_contains(index, "transactional-new-token", 1));
      CHECK(tiny_search_index_add(index, &addition, 1));
      CHECK(candidate_contains(index, "transactional-new-token", 1));
      tiny_search_index_free(index);
      CHECK(tiny_allocator_outstanding() == baseline_allocations);
    }
  CHECK(reached_success);
}

int
main(void)
{
  tiny_allocator_reset();
  CHECK(tiny_allocator_outstanding() == 0);
  test_collision_rehash_and_unicode();
  CHECK(tiny_allocator_outstanding() == 0);
  test_transactional_allocation_failure();
  tiny_allocator_reset();
  CHECK(tiny_allocator_outstanding() == 0);
  return failures == 0 ? 0 : 1;
}