TestFixedArray.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <LibTest/TestSuite.h>
  8. #include <AK/FixedArray.h>
  9. #include <AK/NoAllocationGuard.h>
  10. TEST_CASE(construct)
  11. {
  12. EXPECT_EQ(FixedArray<int>().size(), 0u);
  13. EXPECT_EQ(FixedArray<int>::must_create_but_fixme_should_propagate_errors(1985).size(), 1985u);
  14. }
  15. TEST_CASE(ints)
  16. {
  17. FixedArray<int> ints = FixedArray<int>::must_create_but_fixme_should_propagate_errors(3);
  18. ints[0] = 0;
  19. ints[1] = 1;
  20. ints[2] = 2;
  21. EXPECT_EQ(ints[0], 0);
  22. EXPECT_EQ(ints[1], 1);
  23. EXPECT_EQ(ints[2], 2);
  24. }
  25. TEST_CASE(swap)
  26. {
  27. FixedArray<int> first = FixedArray<int>::must_create_but_fixme_should_propagate_errors(4);
  28. FixedArray<int> second = FixedArray<int>::must_create_but_fixme_should_propagate_errors(5);
  29. first[3] = 1;
  30. second[3] = 2;
  31. first.swap(second);
  32. EXPECT_EQ(first.size(), 5u);
  33. EXPECT_EQ(second.size(), 4u);
  34. EXPECT_EQ(first[3], 2);
  35. EXPECT_EQ(second[3], 1);
  36. }
  37. TEST_CASE(move)
  38. {
  39. FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
  40. FixedArray<int> moved_to_array(move(moved_from_array));
  41. EXPECT_EQ(moved_to_array.size(), 6u);
  42. EXPECT_EQ(moved_from_array.size(), 0u);
  43. }
  44. TEST_CASE(no_allocation)
  45. {
  46. FixedArray<int> array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(5);
  47. EXPECT_NO_CRASH("Assignments", [&] {
  48. NoAllocationGuard guard;
  49. array[0] = 0;
  50. array[1] = 1;
  51. array[2] = 2;
  52. array[4] = array[1];
  53. array[3] = array[0] + array[2];
  54. return Test::Crash::Failure::DidNotCrash;
  55. });
  56. EXPECT_NO_CRASH("Move", [&] {
  57. FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
  58. NoAllocationGuard guard;
  59. FixedArray<int> moved_to_array(move(moved_from_array));
  60. // We need to ensure that this destructor runs before the FixedArray destructor.
  61. guard.~NoAllocationGuard();
  62. return Test::Crash::Failure::DidNotCrash;
  63. });
  64. EXPECT_NO_CRASH("Swap", [&] {
  65. FixedArray<int> target_for_swapping;
  66. {
  67. NoAllocationGuard guard;
  68. array.swap(target_for_swapping);
  69. }
  70. return Test::Crash::Failure::DidNotCrash;
  71. });
  72. }