array_stack_test.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/array_stack.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. namespace Carbon::Testing {
  8. namespace {
  9. using ::testing::ElementsAre;
  10. using ::testing::IsEmpty;
  11. TEST(ArrayStack, Basics) {
  12. ArrayStack<int> stack;
  13. // PeekAllValues is valid when there are no arrays.
  14. EXPECT_THAT(stack.PeekAllValues(), IsEmpty());
  15. // An array starts empty.
  16. stack.PushArray();
  17. EXPECT_THAT(stack.PeekArray(), IsEmpty());
  18. EXPECT_THAT(stack.PeekAllValues(), IsEmpty());
  19. // Pushing a couple values works.
  20. stack.AppendToTop(1);
  21. stack.AppendToTop(2);
  22. EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
  23. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
  24. // Pushing a new array starts empty, old values are still there.
  25. stack.PushArray();
  26. EXPECT_THAT(stack.PeekArray(), IsEmpty());
  27. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
  28. // The added value goes to the 2nd array.
  29. stack.AppendToTop(3);
  30. EXPECT_THAT(stack.PeekArray(), ElementsAre(3));
  31. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2, 3));
  32. // Popping goes back to the 1st array.
  33. stack.PopArray();
  34. EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
  35. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
  36. // Push a couple arrays, then a value on the now-3rd array.
  37. stack.PushArray();
  38. stack.PushArray();
  39. stack.AppendToTop(4);
  40. EXPECT_THAT(stack.PeekArray(), ElementsAre(4));
  41. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2, 4));
  42. // Popping the 3rd array goes to the 2nd array, which is empty.
  43. stack.PopArray();
  44. EXPECT_THAT(stack.PeekArray(), IsEmpty());
  45. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
  46. // Again back to the 1st array.
  47. stack.PopArray();
  48. EXPECT_THAT(stack.PeekArray(), ElementsAre(1, 2));
  49. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(1, 2));
  50. // Go down to no arrays.
  51. stack.PopArray();
  52. EXPECT_THAT(stack.PeekAllValues(), IsEmpty());
  53. // Add a new 1st array.
  54. stack.PushArray();
  55. stack.AppendToTop(5);
  56. EXPECT_THAT(stack.PeekArray(), ElementsAre(5));
  57. EXPECT_THAT(stack.PeekAllValues(), ElementsAre(5));
  58. }
  59. } // namespace
  60. } // namespace Carbon::Testing