-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathConsoleTests.cpp
More file actions
194 lines (145 loc) · 4.17 KB
/
ConsoleTests.cpp
File metadata and controls
194 lines (145 loc) · 4.17 KB
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
#include "Precompile.h"
#include "Platform/Console.h"
#include "Platform/Assert.h"
#include "gtest/gtest.h"
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace Helium;
void setupFileScanTest(const char* const fileName, const char* strings[], size_t stringCount)
{
std::ofstream fileToCheck(fileName);
for (size_t i = 0; i < stringCount; i++)
{
fileToCheck << strings[i];
}
fileToCheck.close();
}
TEST(PlatformConsoleTest, CheckScanFromFile )
{
const char* const fileName = "scan_test1.txt";
const char* strings[] = { "testing123", "\n", "54321" };
setupFileScanTest(fileName, strings, 3);
FILE* fileToScan = fopen(fileName, "r");
char scanedText[64];
int res = FileScan(fileToScan, "%s", scanedText);
ASSERT_TRUE(res == 1);
ASSERT_TRUE(strcmp(strings[0], scanedText) == 0);
int num = 0;
res = FileScan(fileToScan, "%d", &num);
ASSERT_TRUE(res == 1);
ASSERT_TRUE(num == 54321);
fclose(fileToScan);
remove(fileName);
}
TEST(PlatformConsoleTest, CheckScanFromFile2)
{
const char* const fileName = "scan_test2.txt";
// the only diferentce between CheckScanFromFile test
const char* strings[] = { "testing123", " " /*no new line*/, "54321" };
setupFileScanTest(fileName, strings, 3);
FILE* fileToScan = fopen(fileName, "r");
char scanedText[64];
int res = FileScan(fileToScan, "%s", scanedText);
ASSERT_TRUE(res == 1);
ASSERT_TRUE(strcmp(strings[0], scanedText) == 0);
int num = 0;
FileScan(fileToScan, "%d", &num);
ASSERT_TRUE(num == 0);
fclose(fileToScan);
remove(fileName);
}
TEST(PlatformConsoleTest, CheckScanFromFile3)
{
const char* const fileName = "scan_test2.txt";
// the only diferentce between CheckScanFromFile test
const char* strings[] = { "testing123", " " /*no new line*/, "54321" };
setupFileScanTest(fileName, strings, 3);
FILE* fileToScan = fopen(fileName, "r");
char scanedText[64];
int res = FileScan(fileToScan, "", scanedText);
fclose(fileToScan);
remove(fileName);
}
TEST(PlatformConsoleTest, CheckStringScan)
{
int num = 0;
char str[10];
StringScan("9876 foobar", "%d %s", &num, str);
ASSERT_TRUE(num == 9876);
ASSERT_TRUE(strcmp(str, "foobar") == 0);
}
TEST(PlatformConsoleTest, CheckFilePrint)
{
const char* const fileName = "print_test.txt";
FILE* file = fopen(fileName, "w");
ASSERT_TRUE(file != NULL);
int res = FilePrint(file, L"testing %d", 1234);
ASSERT_TRUE(res != 0);
ASSERT_TRUE(fclose(file) == 0);
std::wifstream fileToCheck(fileName);
ASSERT_TRUE(fileToCheck.is_open());
std::wstring text(L"");
int num = 0;
fileToCheck >> text >> num;
fileToCheck.close();
ASSERT_TRUE(num == 1234);
ASSERT_TRUE(wcscmp(text.c_str(), L"testing") == 0);
}
TEST(PlatformConsoleTest, CheckStringPrint)
{
const int size = 10;
char dest[size];
int res = StringPrint(dest, size, "foobar %d", size);
ASSERT_TRUE(res != 0);
int num = 0;
char str[size];
StringScan(dest, "%s %d", str, &num);
ASSERT_TRUE(num == size);
ASSERT_TRUE(strcmp(str, "foobar") == 0);
}
TEST(PlatformConsoleTest, CheckStringPrintWithSizeDeduction)
{
const int size = 10;
char dest[size];
int res = StringPrint(dest, "barbaz %d", size);
ASSERT_TRUE(res != 0);
int num = 0;
char str[size];
StringScan(dest, "%s %d", str, &num);
ASSERT_TRUE(num == size);
ASSERT_TRUE(strcmp(str, "barbaz") == 0);
}
TEST(PlatformConsoleTest, CheckStringPrintOverSize)
{
const int size = 10;
char dest[size];
int res = StringPrint(dest, size, "foobar bar baz %d", 33);
char str1[size];
char str2[size];
StringScan(dest, "%s %s", str1, str2);
ASSERT_TRUE(strcmp(str1, "foobar") == 0);
ASSERT_TRUE(strcmp(str2, "ba") == 0);
}
#if HELIUM_ASSERT_ENABLED
TEST(PlatformConsoleTest, FailOnWCharScan)
{
ASSERT_DEATH(Scan(L""), "");
}
TEST(PlatformConsoleTest, CheckScanFromNullFile)
{
FILE* fileToScan = nullptr;
char scanedText[64];
ASSERT_DEATH(FileScan(fileToScan, "%s", scanedText), "");
}
TEST(PlatformConsoleTest, CheckPrintToNullFile)
{
FILE* file = nullptr;
char scanedText[64];
ASSERT_DEATH(FilePrint(file, "123 %s", scanedText), "");
}
TEST(PlatformConsoleTest, FailOnWCharFileScan)
{
ASSERT_DEATH(FileScan(nullptr, L""), "");
}
#endif