aligned_ut.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "aligned.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. class TNastyInputStream: public IInputStream {
  4. public:
  5. TNastyInputStream()
  6. : Pos_(0)
  7. {
  8. }
  9. protected:
  10. size_t DoRead(void* buf, size_t len) override {
  11. if (len == 0) {
  12. return 0;
  13. }
  14. *static_cast<unsigned char*>(buf) = static_cast<unsigned char>(Pos_);
  15. ++Pos_;
  16. return 1;
  17. }
  18. size_t DoSkip(size_t len) override {
  19. if (len == 0) {
  20. return 0;
  21. }
  22. ++Pos_;
  23. return 1;
  24. }
  25. private:
  26. size_t Pos_;
  27. };
  28. Y_UNIT_TEST_SUITE(TAlignedTest) {
  29. Y_UNIT_TEST(AlignInput) {
  30. TNastyInputStream input0;
  31. TAlignedInput alignedInput(&input0);
  32. char c = '\1';
  33. alignedInput.Align(2);
  34. alignedInput.ReadChar(c);
  35. UNIT_ASSERT_VALUES_EQUAL(c, '\x0');
  36. alignedInput.Align(2);
  37. alignedInput.ReadChar(c);
  38. UNIT_ASSERT_VALUES_EQUAL(c, '\x2');
  39. alignedInput.Align(4);
  40. alignedInput.ReadChar(c);
  41. UNIT_ASSERT_VALUES_EQUAL(c, '\x4');
  42. alignedInput.Align(16);
  43. alignedInput.ReadChar(c);
  44. UNIT_ASSERT_VALUES_EQUAL(c, '\x10');
  45. alignedInput.Align(128);
  46. alignedInput.ReadChar(c);
  47. UNIT_ASSERT_VALUES_EQUAL(c, '\x80');
  48. }
  49. }