fixed_vector.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include<__vic/defs.h>
  2. #include<__vic/fixed_vector.h>
  3. #include<iostream>
  4. #include<exception>
  5. #include<string>
  6. #include<cassert>
  7. namespace tests {
  8. class C : private __vic::non_copyable
  9. {
  10. std::string name_;
  11. public:
  12. explicit C(const std::string &name) : name_(name) {}
  13. std::string name() const { return name_; }
  14. };
  15. void run()
  16. {
  17. __vic::fixed_vector<C> v(2);
  18. std::cout << "Vector size: " << v.size() << '\n';
  19. new(v.alloc()) C("C1");
  20. assert(v.size() == 0);
  21. v.push_allocated();
  22. assert(v.size() == 1);
  23. #if __cpp_rvalue_references && __cpp_variadic_templates
  24. v.emplace_back("C2");
  25. #else
  26. new(v.alloc()) C("C2");
  27. v.push_allocated();
  28. #endif
  29. assert(v.size() == 2);
  30. std::cout << "Vector size: " << v.size() << '\n';
  31. std::cout << "Access by index:\n";
  32. for(unsigned i = 0; i < v.size(); i++)
  33. std::cout << v[i].name() << ' ';
  34. std::cout << '\n';
  35. std::cout << "Access by iterator:\n";
  36. for(__vic::fixed_vector<C>::const_iterator it =
  37. v.cbegin(); it != v.cend(); ++it)
  38. std::cout << it->name() << ' ';
  39. std::cout << '\n';
  40. }
  41. } // namespace
  42. int main()
  43. {
  44. try
  45. {
  46. tests::run();
  47. return 0;
  48. }
  49. catch(const std::exception &ex)
  50. {
  51. std::cerr << ex.what() << '\n';
  52. }
  53. return 1;
  54. }