summaryrefslogtreecommitdiff
path: root/test/core/utils/BufferedCharReaderTest.cpp
blob: 69c09743a893703571563a7e4f9ff430e0aa45d1 (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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
    SCAENEA IDL Compiler (scidlc)
    Copyright (C) 2014  Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <string>
#include <iostream>

#include "gtest/gtest.h"

#include <core/utils/BufferedCharReader.hpp>

namespace ousia{
namespace utils{

TEST(BufferedCharReaderTest, SimpleReadTest)
{
	const std::string testStr("this is a test");
	char c;

	// Feed a test string into the reader
	BufferedCharReader reader;
	reader.feed(testStr);
	reader.close();

	// Try to read the test string
	std::string res;
	while (!reader.atEnd()) {
		ASSERT_TRUE(reader.read(&c));
		res.append(&c, 1);
	}

	// The two strings must equal
	ASSERT_STREQ(testStr.c_str(), res.c_str()) ;

	// We must now be at line 1, column 15
	ASSERT_EQ(1, reader.getLine());
	ASSERT_EQ(testStr.size() + 1, reader.getColumn());

	// If we call either read or peek, false is returned
	ASSERT_FALSE(reader.read(&c));
	ASSERT_FALSE(reader.peek(&c));
}

TEST(BufferedCharReaderTest, SimplePeekTest)
{
	const std::string testStr("this is a test");
	char c;

	// Feed a test string into the reader
	BufferedCharReader reader;
	reader.feed(testStr);
	reader.close();

	// Try to read the test string
	std::string res;
	while (reader.peek(&c)) {
		res.append(&c, 1);
	}

	// Peeking does not trigger the "atEnd" flag
	ASSERT_FALSE(reader.atEnd());

	// The two strings must equal
	ASSERT_STREQ(testStr.c_str(), res.c_str());

	// We must now be at line 1, column 1 and NOT at the end of the stream
	ASSERT_EQ(1, reader.getLine());
	ASSERT_EQ(1, reader.getColumn());
	ASSERT_FALSE(reader.atEnd());

	// If we consume the peek, we must be at line 1, column 15 and we should be
	// at the end of the stream
	reader.consumePeek();
	ASSERT_EQ(1, reader.getLine());
	ASSERT_EQ(testStr.size() + 1, reader.getColumn());
	ASSERT_TRUE(reader.atEnd());

	// If we call either read or peek, false is returned
	ASSERT_FALSE(reader.read(&c));
	ASSERT_FALSE(reader.peek(&c));
}

TEST(BufferedCharReaderTest, SplittedPeakTest)
{
	const std::string testStr("this is a test");
	char c;

	// Feed a test string into the reader
	BufferedCharReader reader;

	// Try to peek the test string, feed char after char into the reader
	std::string res;
	for (unsigned int i = 0; i < testStr.length(); i++) {
		reader.feed(std::string(&testStr[i], 1));
		while (reader.peek(&c)) {
			res.append(&c, 1);
		}
	}
	reader.close();

	// Consume the peeked data
	ASSERT_FALSE(reader.atEnd());
	reader.consumePeek();
	ASSERT_TRUE(reader.atEnd());

	// The two strings must equal
	ASSERT_STREQ(testStr.c_str(), res.c_str()) ;

	// We must now be at line 1, column 15
	ASSERT_EQ(1, reader.getLine());
	ASSERT_EQ(testStr.size() + 1, reader.getColumn());

	// If we call either read or peek, false is returned
	ASSERT_FALSE(reader.read(&c));
	ASSERT_FALSE(reader.peek(&c));
}

TEST(BufferedCharReaderTest, RowColumnCounterTest)
{
	const std::string testStr("1\n\r2\n3\r\n\n4");
	char c;

	// Feed a test string into the reader
	BufferedCharReader reader;
	reader.feed(testStr);
	reader.close();

	// We should currently be in line 1, column 1
	ASSERT_EQ(1, reader.getLine());
	ASSERT_EQ(1, reader.getColumn());

	// Read two characters
	for (int i = 0; i < 2; i++) reader.read(&c);
	ASSERT_EQ(2, reader.getLine());
	ASSERT_EQ(1, reader.getColumn());

	// Read two characters
	for (int i = 0; i < 2; i++) reader.read(&c);
	ASSERT_EQ(3, reader.getLine());
	ASSERT_EQ(1, reader.getColumn());

	// Read three characters
	for (int i = 0; i < 3; i++) reader.read(&c);
	ASSERT_EQ(5, reader.getLine());
	ASSERT_EQ(1, reader.getColumn());
}

TEST(BufferedCharReaderTest, LinebreakSubstitutionTest)
{
	const std::string testStr("this\n\ris\n\rjust\na test\r\n\rtest\n\r");
	const std::string expStr("this\nis\njust\na test\n\ntest\n");

	// Feed a test string into the reader
	BufferedCharReader reader;
	reader.feed(testStr);

	// Read all characters from the test string
	std::string res;
	char c;
	while (reader.read(&c)) {
		res.append(&c, 1);
	}

	// Test for equality
	ASSERT_STREQ(expStr.c_str(), res.c_str());
}

TEST(BufferedCharReaderTest, RowColumnCounterUTF8Test)
{
	// Create a test string with some umlauts
	const std::string testStr("\x61\xc3\x96\xc3\x84\xc3\x9c\xc3\x9f");
	char c;

	// Feed a test string into the reader
	BufferedCharReader reader;
	reader.feed(testStr);
	reader.close();

	// Read all bytes
	while (reader.read(&c));

	// The sequence above equals 5 UTF-8 characters (so after reading all the
	// cursor is at position 6)
	ASSERT_EQ(1, reader.getLine());
	ASSERT_EQ(6, reader.getColumn());
}

}
}