aboutsummaryrefslogtreecommitdiff
path: root/test/base/io/MemoryStreamTest.cpp
blob: 684971b1c1107c2b25449a5c62c0dafe3e8a76e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "cru/base/io/MemoryStream.h"
#include "cru/base/io/Stream.h"

#include <catch2/catch_test_macros.hpp>

#include <algorithm>
#include <cstddef>
#include <ranges>

TEST_CASE("MemoryStream should work.", "[io][stream]") {
  using namespace cru::io;
  const int size = 100;
  std::vector<std::byte> buffer(size);

  buffer[1] = std::byte(0xf0);
  buffer[2] = std::byte(0x0f);

  MemoryStream stream(buffer.data(), size, true);

  REQUIRE(stream.CanRead());
  REQUIRE(!stream.CanWrite());

  std::vector<std::byte> buffer2(size * 2);

  auto read = stream.Read(buffer2.data(), buffer2.size());
  auto read2 = stream.Read(buffer2.data(), 1);
  REQUIRE(read == size);
  REQUIRE(std::ranges::equal(buffer, buffer2 | std::views::take(size)));
  REQUIRE(read2 == Stream::kEOF);
}