reverse.pass.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <algorithm>
  9. // template<BidirectionalIterator Iter>
  10. // requires HasSwap<Iter::reference, Iter::reference>
  11. // void
  12. // reverse(Iter first, Iter last);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. template <class Iter>
  18. void
  19. test()
  20. {
  21. int ia[] = {0};
  22. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  23. std::reverse(Iter(ia), Iter(ia));
  24. assert(ia[0] == 0);
  25. std::reverse(Iter(ia), Iter(ia+sa));
  26. assert(ia[0] == 0);
  27. int ib[] = {0, 1};
  28. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  29. std::reverse(Iter(ib), Iter(ib+sb));
  30. assert(ib[0] == 1);
  31. assert(ib[1] == 0);
  32. int ic[] = {0, 1, 2};
  33. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  34. std::reverse(Iter(ic), Iter(ic+sc));
  35. assert(ic[0] == 2);
  36. assert(ic[1] == 1);
  37. assert(ic[2] == 0);
  38. int id[] = {0, 1, 2, 3};
  39. const unsigned sd = sizeof(id)/sizeof(id[0]);
  40. std::reverse(Iter(id), Iter(id+sd));
  41. assert(id[0] == 3);
  42. assert(id[1] == 2);
  43. assert(id[2] == 1);
  44. assert(id[3] == 0);
  45. }
  46. int main(int, char**)
  47. {
  48. test<bidirectional_iterator<int*> >();
  49. test<random_access_iterator<int*> >();
  50. test<int*>();
  51. return 0;
  52. }