| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "courgette/third_party/bsdiff/qsufsort.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cstddef> | |
| 9 #include <cstring> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 bool IsPermutation(const std::vector<int> v) { | |
| 18 std::vector<int> v_sorted(v); | |
| 19 std::sort(v_sorted.begin(), v_sorted.end()); | |
| 20 for (int i = 0; i < static_cast<int>(v.size()); ++i) | |
| 21 if (i != v_sorted[i]) | |
| 22 return false; | |
| 23 return true; | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 TEST(QSufSortTest, Sort) { | |
| 29 const char* test_cases[] = { | |
| 30 "", | |
| 31 "a", | |
| 32 "za", | |
| 33 "CACAO", | |
| 34 "banana", | |
| 35 "tobeornottobe", | |
| 36 "The quick brown fox jumps over the lazy dog.", | |
| 37 "elephantelephantelephantelephantelephant", | |
| 38 "-------------------------", | |
| 39 "011010011001011010010110011010010", | |
| 40 "3141592653589793238462643383279502884197169399375105", | |
| 41 "\xFF\xFE\xFF\xFE\xFD\x80\x30\x31\x32\x80\x30\xFF\x01\xAB\xCD", | |
| 42 }; | |
| 43 | |
| 44 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { | |
| 45 int size = static_cast<int>(::strlen(test_cases[idx])); | |
| 46 const unsigned char* buf = | |
| 47 reinterpret_cast<const unsigned char*>(test_cases[idx]); | |
| 48 | |
| 49 // Generate the suffix array as I. | |
| 50 std::vector<int> I(size + 1); | |
| 51 std::vector<int> V(size + 1); | |
| 52 qsuf::qsufsort<int*>(&I[0], &V[0], buf, size); | |
| 53 | |
| 54 // Expect I[] and V[] to be a permutation of [0, size]. | |
| 55 EXPECT_TRUE(IsPermutation(I)); | |
| 56 EXPECT_TRUE(IsPermutation(V)); | |
| 57 | |
| 58 // Expect V[] to be inverse of I[]. | |
| 59 for (int i = 0; i < size + 1; ++i) | |
| 60 EXPECT_EQ(i, V[I[i]]); | |
| 61 | |
| 62 // First string must be empty string. | |
| 63 EXPECT_EQ(size, I[0]); | |
| 64 | |
| 65 // Expect that the |size + 1| suffixes are strictly ordered. | |
| 66 const unsigned char* end = buf + size; | |
| 67 for (int i = 0; i < size; ++i) { | |
| 68 const unsigned char* suf1 = buf + I[i]; | |
| 69 const unsigned char* suf2 = buf + I[i + 1]; | |
| 70 bool is_less = std::lexicographical_compare(suf1, end, suf2, end); | |
| 71 EXPECT_TRUE(is_less); | |
| 72 } | |
| 73 } | |
| 74 } | |
| OLD | NEW |