TestBinarySearch.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/TestSuite.h>
  27. #include <AK/BinarySearch.h>
  28. #include <AK/Span.h>
  29. #include <cstring>
  30. #include <new>
  31. TEST_CASE(vector_ints)
  32. {
  33. Vector<int> ints;
  34. ints.append(1);
  35. ints.append(2);
  36. ints.append(3);
  37. auto test1 = *binary_search(ints.span(), 1, AK::integral_compare<int>);
  38. auto test2 = *binary_search(ints.span(), 2, AK::integral_compare<int>);
  39. auto test3 = *binary_search(ints.span(), 3, AK::integral_compare<int>);
  40. EXPECT_EQ(test1, 1);
  41. EXPECT_EQ(test2, 2);
  42. EXPECT_EQ(test3, 3);
  43. }
  44. TEST_CASE(array_doubles)
  45. {
  46. double doubles[] = { 1.1, 9.9, 33.33 };
  47. auto test1 = *binary_search(Span<double> { doubles, 3 }, 1.1, AK::integral_compare<double>);
  48. auto test2 = *binary_search(Span<double> { doubles, 3 }, 9.9, AK::integral_compare<double>);
  49. auto test3 = *binary_search(Span<double> { doubles, 3 }, 33.33, AK::integral_compare<double>);
  50. EXPECT_EQ(test1, 1.1);
  51. EXPECT_EQ(test2, 9.9);
  52. EXPECT_EQ(test3, 33.33);
  53. }
  54. TEST_CASE(vector_strings)
  55. {
  56. Vector<String> strings;
  57. strings.append("bat");
  58. strings.append("cat");
  59. strings.append("dog");
  60. auto string_compare = [](const String& a, const String& b) -> int {
  61. return strcmp(a.characters(), b.characters());
  62. };
  63. auto test1 = *binary_search(strings.span(), String("bat"), string_compare);
  64. auto test2 = *binary_search(strings.span(), String("cat"), string_compare);
  65. auto test3 = *binary_search(strings.span(), String("dog"), string_compare);
  66. EXPECT_EQ(test1, String("bat"));
  67. EXPECT_EQ(test2, String("cat"));
  68. EXPECT_EQ(test3, String("dog"));
  69. }
  70. TEST_CASE(single_element)
  71. {
  72. Vector<int> ints;
  73. ints.append(1);
  74. auto test1 = *binary_search(ints.span(), 1, AK::integral_compare<int>);
  75. EXPECT_EQ(test1, 1);
  76. }
  77. TEST_CASE(not_found)
  78. {
  79. Vector<int> ints;
  80. ints.append(1);
  81. ints.append(2);
  82. ints.append(3);
  83. auto test1 = binary_search(ints.span(), -1, AK::integral_compare<int>);
  84. auto test2 = binary_search(ints.span(), 0, AK::integral_compare<int>);
  85. auto test3 = binary_search(ints.span(), 4, AK::integral_compare<int>);
  86. EXPECT_EQ(test1, nullptr);
  87. EXPECT_EQ(test2, nullptr);
  88. EXPECT_EQ(test3, nullptr);
  89. }
  90. TEST_CASE(no_elements)
  91. {
  92. Vector<int> ints;
  93. auto test1 = binary_search(ints.span(), 1, AK::integral_compare<int>);
  94. EXPECT_EQ(test1, nullptr);
  95. }
  96. TEST_CASE(huge_char_array)
  97. {
  98. const size_t N = 2147483680;
  99. Bytes span { new (std::nothrow) u8[N], N };
  100. EXPECT(span.data() != nullptr);
  101. for (size_t i = 0; i < span.size(); ++i)
  102. span[i] = 'a';
  103. size_t index = N - 1;
  104. for (u8 c = 'z'; c > 'b'; --c)
  105. span[index--] = c;
  106. EXPECT_EQ(span[N - 1], 'z');
  107. const u8 a = 'a';
  108. auto where = binary_search(span, a, AK::integral_compare<u8>);
  109. EXPECT(where != nullptr);
  110. EXPECT_EQ(*where, 'a');
  111. const u8 z = 'z';
  112. where = binary_search(span, z, AK::integral_compare<u8>);
  113. EXPECT(where != nullptr);
  114. EXPECT_EQ(*where, 'z');
  115. size_t near = 0;
  116. const u8 tilde = '~';
  117. where = binary_search(span, tilde, AK::integral_compare<u8>, &near);
  118. EXPECT_EQ(where, nullptr);
  119. EXPECT_EQ(near, N - 1);
  120. const u8 b = 'b';
  121. where = binary_search(span, b, AK::integral_compare<u8>, &near);
  122. EXPECT_EQ(where, nullptr);
  123. EXPECT_EQ(near, N - ('z' - b + 1));
  124. delete[] span.data();
  125. }
  126. TEST_MAIN(BinarySearch)