utils_unit.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #include <gtest/gtest.h>
  19. #include <stdlib.h>
  20. #include <algorithm>
  21. #include "include/doclist_compression.h"
  22. #include "include/index_wrapper.h"
  23. #include "unittest_util.h"
  24. namespace wwsearch {
  25. class UnitTest : public ::testing::Test {
  26. public:
  27. public:
  28. UnitTest() {}
  29. static void SetUpTestCase() {}
  30. static void TearDownTestCase() {}
  31. virtual void SetUp() override {}
  32. virtual void TearDown() override {}
  33. private:
  34. };
  35. int DescSort(const uint64_t &a, const uint64_t &b) { return b < a; }
  36. void GetOrderDocumentIDList(std::vector<uint64_t> &doc_list, size_t n) {
  37. srandom(time(NULL));
  38. for (size_t i = 0; i < n; i++) {
  39. uint64_t doc_id;
  40. doc_id = (static_cast<uint64_t>(random()) << 32) + random();
  41. doc_list.push_back(doc_id);
  42. }
  43. std::sort(doc_list.begin(), doc_list.end(), DescSort);
  44. }
  45. TEST_F(UnitTest, CompressionDocList) {
  46. srandom(time(NULL));
  47. for (size_t i = 0; i < 10; i++) {
  48. size_t len = random() % 10000 + 1;
  49. std::vector<uint64_t> doc_list;
  50. GetOrderDocumentIDList(doc_list, len);
  51. std::string buffer;
  52. buffer.reserve(9 * len);
  53. DocListCompressionEncoder encoder;
  54. DocListCompressionDecoder decoder;
  55. for (size_t j = 0; j < len; j++) {
  56. int delete_flag = random() % 2;
  57. DocumentID doc_id = doc_list[j];
  58. buffer.append((const char *)&doc_id, 8);
  59. DocumentState state = 0;
  60. if (delete_flag) {
  61. state = 1;
  62. }
  63. buffer.append((const char *)&state, 1);
  64. ASSERT_TRUE(true == encoder.AddDoc(doc_id, delete_flag));
  65. }
  66. std::string buffer1, buffer2;
  67. ASSERT_TRUE(true == encoder.SerializeToString(buffer1));
  68. ASSERT_TRUE(true == decoder.Decode(buffer1, buffer2));
  69. // skip header and do compare
  70. ASSERT_TRUE(buffer.size() == (buffer2.size() - 1));
  71. ASSERT_TRUE(0 == memcmp(buffer.c_str(), (char *)(buffer2.c_str() + 1),
  72. buffer.size()));
  73. }
  74. }
  75. } // namespace wwsearch