tests_ut.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <library/cpp/containers/stack_array/stack_array.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TestStackArray) {
  4. using namespace NStackArray;
  5. static inline void* FillWithTrash(void* d, size_t l) {
  6. memset(d, 0xCC, l);
  7. return d;
  8. }
  9. #define ALLOC(type, len) FillWithTrash(alloca(sizeof(type) * len), sizeof(type) * len), len
  10. Y_UNIT_TEST(Test1) {
  11. TStackArray<ui32> s(ALLOC(ui32, 10));
  12. UNIT_ASSERT_VALUES_EQUAL(s.size(), 10);
  13. for (size_t i = 0; i < s.size(); ++i) {
  14. UNIT_ASSERT_VALUES_EQUAL(s[i], 0xCCCCCCCC);
  15. }
  16. for (auto&& x : s) {
  17. UNIT_ASSERT_VALUES_EQUAL(x, 0xCCCCCCCC);
  18. }
  19. for (size_t i = 0; i < s.size(); ++i) {
  20. s[i] = i;
  21. }
  22. size_t ss = 0;
  23. for (auto&& x : s) {
  24. ss += x;
  25. }
  26. UNIT_ASSERT_VALUES_EQUAL(ss, 45);
  27. }
  28. static int N1 = 0;
  29. struct TX1 {
  30. inline TX1() {
  31. ++N1;
  32. }
  33. inline ~TX1() {
  34. --N1;
  35. }
  36. };
  37. Y_UNIT_TEST(Test2) {
  38. {
  39. TStackArray<TX1> s(ALLOC(TX1, 10));
  40. UNIT_ASSERT_VALUES_EQUAL(N1, 10);
  41. }
  42. UNIT_ASSERT_VALUES_EQUAL(N1, 0);
  43. }
  44. static int N2 = 0;
  45. static int N3 = 0;
  46. struct TX2 {
  47. inline TX2() {
  48. if (N2 >= 5) {
  49. ythrow yexception() << "ups";
  50. }
  51. ++N3;
  52. ++N2;
  53. }
  54. inline ~TX2() {
  55. --N2;
  56. }
  57. };
  58. Y_UNIT_TEST(Test3) {
  59. bool haveException = false;
  60. try {
  61. TStackArray<TX2> s(ALLOC_ON_STACK(TX2, 10));
  62. } catch (...) {
  63. haveException = true;
  64. }
  65. UNIT_ASSERT(haveException);
  66. UNIT_ASSERT_VALUES_EQUAL(N2, 0);
  67. UNIT_ASSERT_VALUES_EQUAL(N3, 5);
  68. }
  69. }