-
Notifications
You must be signed in to change notification settings - Fork 2
/
input_buffer_test.cpp
70 lines (54 loc) · 1.86 KB
/
input_buffer_test.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "buffer.h"
#include <fcntl.h>
#include <unistd.h>
#include "file_data_source.h"
#include <catch2/catch.hpp>
void checked_write(int fd, const void *buffer, size_t size)
{
int rc = write(fd, buffer, size);
REQUIRE(size >= 0);
REQUIRE(static_cast<size_t>(rc) == size);
}
TEST_CASE("InputStreamBuffer")
{
// Generate test file
int fd = open("testfile1", O_WRONLY|O_CREAT|O_TRUNC, 0777);
char buffer1[26];
for(unsigned int i = 0; i < 26; ++i)
buffer1[i] = 'A' + i;
checked_write(fd, buffer1, 26);
checked_write(fd, buffer1, 26);
checked_write(fd, buffer1, 26);
checked_write(fd, buffer1, 26);
close(fd);
FileDataSource data_source;
data_source.Open("testfile1");
InputStreamBuffer input(10, 3);
input.SetSource(&data_source);
input.EnsureAvailable(4);
REQUIRE(input.GetOffset() == 0);
REQUIRE(memcmp(input.GetPointer(), "ABCD", 4) == 0);
REQUIRE(memcmp(input.GetPointer(), "ABCD", 4) == 0);
input.Advance(2);
REQUIRE(input.GetOffset() == 2);
input.EnsureAvailable(6);
REQUIRE(memcmp(input.GetPointer(), "CDEFGH", 6) == 0);
input.Advance(20);
REQUIRE(input.GetOffset() == 22);
input.EnsureAvailable(4);
REQUIRE(memcmp(input.GetPointer(), "WXYZ", 4) == 0);
input.Advance(52);
REQUIRE(input.GetOffset() == 74);
input.EnsureAvailable(4);
REQUIRE(memcmp(input.GetPointer(), "WXYZ", 4) == 0);
input.Advance(29);
REQUIRE_THROWS_AS(input.EnsureAvailable(2), InsufficientDataException);
input.EnsureAvailable(1);
REQUIRE(memcmp(input.GetPointer(), "Z", 1) == 0);
input.Advance(1);
REQUIRE_THROWS_AS(input.EnsureAvailable(1), InsufficientDataException);
REQUIRE_THROWS_AS(input.Rewind(30), InsufficientDataException);
input.Rewind(3);
input.EnsureAvailable(3);
REQUIRE(memcmp(input.GetPointer(), "XYZ", 3) == 0);
}