unittest_util.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "unittest_util.h"
  19. namespace wwsearch {
  20. void InitStringField(IndexField *field, uint16_t field_id,
  21. const std::string &word) {
  22. IndexFieldFlag flag;
  23. flag.SetDocValue();
  24. flag.SetStoredField();
  25. flag.SetTokenize();
  26. flag.SetInvertIndex();
  27. field->SetMeta(field_id, flag);
  28. field->SetString(word);
  29. }
  30. void InitUint32Field(IndexField *field, uint16_t field_id, uint32_t value) {
  31. IndexFieldFlag flag;
  32. flag.SetDocValue();
  33. flag.SetStoredField();
  34. flag.SetTokenize();
  35. flag.SetInvertIndex();
  36. field->SetMeta(field_id, flag);
  37. field->SetUint32(value);
  38. }
  39. void InitUint64Field(IndexField *field, uint16_t field_id, uint64_t value) {
  40. IndexFieldFlag flag;
  41. flag.SetDocValue();
  42. flag.SetStoredField();
  43. flag.SetTokenize();
  44. flag.SetInvertIndex();
  45. field->SetMeta(field_id, flag);
  46. field->SetUint64(value);
  47. }
  48. DocumentUpdater *TestUtil::NewDocument(DocumentID documentid,
  49. std::string str_field,
  50. uint32_t numeric_32, uint64_t numeric_64,
  51. uint32_t sort_value,
  52. uint8_t field_offset) {
  53. DocumentUpdater *du = new DocumentUpdater();
  54. Document &document = du->New();
  55. document.SetID(documentid);
  56. InitStringField(document.AddField(), field_offset + 1, str_field);
  57. InitUint32Field(document.AddField(), field_offset + 2, numeric_32);
  58. InitUint32Field(document.AddField(), field_offset + 3, numeric_64);
  59. InitUint32Field(document.AddField(), field_offset + 4, sort_value);
  60. return du;
  61. }
  62. DocumentUpdater *TestUtil::NewStringFieldDocument(
  63. DocumentID documentid,
  64. const std::vector<TestUtil::FieldIdStrPair> &field_id_str_list) {
  65. DocumentUpdater *du = new DocumentUpdater();
  66. Document &document = du->New();
  67. document.SetID(documentid);
  68. for (auto &field_id_str : field_id_str_list) {
  69. InitStringField(document.AddField(), field_id_str.first,
  70. field_id_str.second);
  71. }
  72. return du;
  73. }
  74. } // namespace wwsearch