TestIndexSequence.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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/StdLibExtras.h>
  27. #include <AK/TestSuite.h>
  28. #include <AK/TypeList.h>
  29. template<typename F, typename... Args>
  30. F for_each_argument(F f, Args&&... args)
  31. {
  32. (f(forward<Args>(args)), ...);
  33. return f;
  34. }
  35. template<typename T, T... ints>
  36. void verify_sequence(IntegerSequence<T, ints...> seq, std::initializer_list<T> expected)
  37. {
  38. EXPECT_EQ(seq.size(), expected.size());
  39. for_each_argument([idx = expected.begin()](T t) mutable { EXPECT_EQ(t, *(idx++)); }, ints...);
  40. }
  41. TEST_CASE(TestIndexSequence)
  42. {
  43. constexpr auto integer_seq1 = IntegerSequence<int, 0, 1, 2, 3, 4> {};
  44. constexpr auto integer_seq2 = MakeIntegerSequence<int, 5> {};
  45. static_assert(IsSame<decltype(integer_seq1), decltype(integer_seq2)>, "");
  46. static_assert(integer_seq1.size() == 5, "");
  47. static_assert(integer_seq2.size() == 5, "");
  48. constexpr auto index_seq1 = IndexSequence<0, 1, 2> {};
  49. constexpr auto index_seq2 = MakeIndexSequence<3> {};
  50. static_assert(IsSame<decltype(index_seq1), decltype(index_seq2)>, "");
  51. verify_sequence(MakeIndexSequence<10> {}, std::initializer_list<unsigned> { 0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 9U });
  52. verify_sequence(MakeIntegerSequence<long, 16> {}, std::initializer_list<long> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
  53. }
  54. TEST_CASE(TypeList)
  55. {
  56. using MyTypes = TypeList<int, bool, char>;
  57. static_assert(IsSame<MyTypes::Type<0>, int>, "");
  58. static_assert(IsSame<MyTypes::Type<1>, bool>, "");
  59. static_assert(IsSame<MyTypes::Type<2>, char>, "");
  60. }
  61. TEST_MAIN(IndexSequence);