Home | History | Annotate | Download | only in tests
      1 #include <sstream>
      2 #include <string>
      3 
      4 #include <marisa_alpha/tail.h>
      5 
      6 #include "assert.h"
      7 
      8 namespace {
      9 
     10 void TestBinaryTail() {
     11   TEST_START();
     12 
     13   marisa_alpha::Tail tail;
     14 
     15   ASSERT(tail.size() == 0);
     16   ASSERT(tail.empty());
     17   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32));
     18 
     19   marisa_alpha::Vector<marisa_alpha::String> keys;
     20   tail.build(keys, NULL, MARISA_ALPHA_BINARY_TAIL);
     21 
     22   ASSERT(tail.size() == 0);
     23   ASSERT(tail.empty());
     24   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32));
     25 
     26   keys.push_back(marisa_alpha::String(""));
     27   marisa_alpha::Vector<marisa_alpha::UInt32> offsets;
     28   tail.build(keys, &offsets, MARISA_ALPHA_BINARY_TAIL);
     29 
     30   ASSERT(tail.size() == 1);
     31   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
     32   ASSERT(!tail.empty());
     33   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
     34   ASSERT(offsets.size() == keys.size() + 1);
     35   ASSERT(offsets[0] == 1);
     36   ASSERT(offsets[1] == tail.size());
     37 
     38   const char binary_key[] = { 'N', 'P', '\0', 'T', 'r', 'i', 'e' };
     39   keys[0] = marisa_alpha::String(binary_key, sizeof(binary_key));
     40   tail.build(keys, &offsets, MARISA_ALPHA_TEXT_TAIL);
     41 
     42   ASSERT(tail.size() == sizeof(binary_key) + 1);
     43   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
     44   ASSERT(!tail.empty());
     45   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
     46   ASSERT(offsets.size() == keys.size() + 1);
     47   ASSERT(offsets[0] == 1);
     48   ASSERT(offsets[1] == tail.size());
     49 
     50   tail.build(keys, &offsets, MARISA_ALPHA_BINARY_TAIL);
     51 
     52   ASSERT(tail.size() == sizeof(binary_key) + 1);
     53   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
     54   ASSERT(!tail.empty());
     55   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
     56   ASSERT(offsets.size() == keys.size() + 1);
     57   ASSERT(offsets[0] == 1);
     58   ASSERT(offsets[1] == tail.size());
     59 
     60   keys.clear();
     61   keys.push_back(marisa_alpha::String("abc"));
     62   keys.push_back(marisa_alpha::String("bc"));
     63   keys.push_back(marisa_alpha::String("abc"));
     64   keys.push_back(marisa_alpha::String("c"));
     65   keys.push_back(marisa_alpha::String("ABC"));
     66   keys.push_back(marisa_alpha::String("AB"));
     67 
     68   tail.build(keys, NULL, MARISA_ALPHA_BINARY_TAIL);
     69 
     70   ASSERT(tail.size() == 15);
     71   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
     72   ASSERT(!tail.empty());
     73   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
     74 
     75   tail.build(keys, &offsets, MARISA_ALPHA_BINARY_TAIL);
     76 
     77   ASSERT(tail.size() == 15);
     78   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
     79   ASSERT(!tail.empty());
     80   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
     81   ASSERT(offsets.size() == 7);
     82   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
     83     ASSERT(marisa_alpha::String(
     84         reinterpret_cast<const char *>(tail[offsets[i]]),
     85         offsets[i + 1] - offsets[i]) == keys[i]);
     86   }
     87 
     88   tail.save("tail-test.dat");
     89   tail.clear();
     90 
     91   ASSERT(tail.size() == 0);
     92   ASSERT(tail.empty());
     93   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32));
     94 
     95   marisa_alpha::Mapper mapper;
     96   tail.mmap(&mapper, "tail-test.dat");
     97 
     98   ASSERT(tail.size() == 15);
     99   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
    100   ASSERT(!tail.empty());
    101   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
    102   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
    103     ASSERT(marisa_alpha::String(
    104         reinterpret_cast<const char *>(tail[offsets[i]]),
    105         offsets[i + 1] - offsets[i]) == keys[i]);
    106   }
    107 
    108   tail.clear();
    109   tail.load("tail-test.dat");
    110 
    111   ASSERT(tail.size() == 15);
    112   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
    113   ASSERT(!tail.empty());
    114   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
    115   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
    116     ASSERT(marisa_alpha::String(
    117         reinterpret_cast<const char *>(tail[offsets[i]]),
    118         offsets[i + 1] - offsets[i]) == keys[i]);
    119   }
    120 
    121   std::stringstream stream;
    122   tail.write(stream);
    123 
    124   tail.clear();
    125   tail.read(stream);
    126 
    127   ASSERT(tail.size() == 15);
    128   ASSERT(tail.mode() == MARISA_ALPHA_BINARY_TAIL);
    129   ASSERT(!tail.empty());
    130   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
    131   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
    132     ASSERT(marisa_alpha::String(
    133         reinterpret_cast<const char *>(tail[offsets[i]]),
    134         offsets[i + 1] - offsets[i]) == keys[i]);
    135   }
    136 
    137   TEST_END();
    138 }
    139 
    140 void TestTextTail() {
    141   TEST_START();
    142 
    143   marisa_alpha::Tail tail;
    144   marisa_alpha::Vector<marisa_alpha::String> keys;
    145   tail.build(keys, NULL, MARISA_ALPHA_TEXT_TAIL);
    146 
    147   ASSERT(tail.size() == 0);
    148   ASSERT(tail.empty());
    149   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32));
    150 
    151   keys.push_back(marisa_alpha::String(""));
    152   marisa_alpha::Vector<marisa_alpha::UInt32> offsets;
    153   tail.build(keys, &offsets, MARISA_ALPHA_TEXT_TAIL);
    154 
    155   ASSERT(tail.size() == 2);
    156   ASSERT(tail.mode() == MARISA_ALPHA_TEXT_TAIL);
    157   ASSERT(!tail.empty());
    158   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
    159   ASSERT(offsets.size() == keys.size());
    160   ASSERT(offsets[0] == 1);
    161   ASSERT(*tail[offsets[0]] == '\0');
    162 
    163   keys.clear();
    164   keys.push_back(marisa_alpha::String("abc"));
    165   keys.push_back(marisa_alpha::String("bc"));
    166   keys.push_back(marisa_alpha::String("abc"));
    167   keys.push_back(marisa_alpha::String("c"));
    168   keys.push_back(marisa_alpha::String("ABC"));
    169   keys.push_back(marisa_alpha::String("AB"));
    170 
    171   tail.build(keys, NULL, MARISA_ALPHA_TEXT_TAIL);
    172 
    173   ASSERT(tail.size() == 12);
    174   ASSERT(tail.mode() == MARISA_ALPHA_TEXT_TAIL);
    175   ASSERT(!tail.empty());
    176   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
    177 
    178   tail.build(keys, &offsets, MARISA_ALPHA_TEXT_TAIL);
    179 
    180   ASSERT(tail.size() == 12);
    181   ASSERT(tail.mode() == MARISA_ALPHA_TEXT_TAIL);
    182   ASSERT(!tail.empty());
    183   ASSERT(tail.total_size() == (sizeof(marisa_alpha::UInt32) + tail.size()));
    184   ASSERT(offsets.size() == keys.size());
    185   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
    186     ASSERT(marisa_alpha::String(reinterpret_cast<const char *>(
    187         tail[offsets[i]])) == keys[i]);
    188   }
    189 
    190   tail.save("tail-test.dat");
    191   tail.clear();
    192 
    193   ASSERT(tail.size() == 0);
    194   ASSERT(tail.empty());
    195   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32));
    196 
    197   marisa_alpha::Mapper mapper;
    198   tail.mmap(&mapper, "tail-test.dat");
    199 
    200   ASSERT(tail.size() == 12);
    201   ASSERT(tail.mode() == MARISA_ALPHA_TEXT_TAIL);
    202   ASSERT(!tail.empty());
    203   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32) + tail.size());
    204   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
    205     ASSERT(marisa_alpha::String(reinterpret_cast<const char *>(
    206         tail[offsets[i]])) == keys[i]);
    207   }
    208 
    209   tail.clear();
    210   tail.load("tail-test.dat");
    211 
    212   ASSERT(tail.size() == 12);
    213   ASSERT(tail.mode() == MARISA_ALPHA_TEXT_TAIL);
    214   ASSERT(!tail.empty());
    215   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32) + tail.size());
    216   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
    217     ASSERT(marisa_alpha::String(reinterpret_cast<const char *>(
    218         tail[offsets[i]])) == keys[i]);
    219   }
    220 
    221   std::stringstream stream;
    222   tail.write(stream);
    223 
    224   tail.clear();
    225   tail.read(stream);
    226 
    227   ASSERT(tail.size() == 12);
    228   ASSERT(tail.mode() == MARISA_ALPHA_TEXT_TAIL);
    229   ASSERT(!tail.empty());
    230   ASSERT(tail.total_size() == sizeof(marisa_alpha::UInt32) + tail.size());
    231   for (marisa_alpha::UInt32 i = 0; i < keys.size(); ++i) {
    232     ASSERT(marisa_alpha::String(reinterpret_cast<const char *>(
    233         tail[offsets[i]])) == keys[i]);
    234   }
    235 
    236   TEST_END();
    237 }
    238 
    239 }  // namespace
    240 
    241 int main() {
    242   TestBinaryTail();
    243   TestTextTail();
    244 
    245   return 0;
    246 }
    247