Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_io_expected_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
38#include <sstream>
39#include <string>
40
41#include <gtest/gtest.h>
42
43#include <ca-io.H>
44
45using namespace Aleph;
46using namespace Aleph::CA;
47
48// --- RLE -------------------------------------------------------------------
49
50TEST(CaIoExpected, RleSuccess)
51{
52 auto r = try_read_rle_string("x = 3, y = 3, rule = B3/S23\nbob$2bo$3o!\n");
53 ASSERT_TRUE(r.has_value()) << (r.has_value() ? "" : r.error());
54 EXPECT_EQ(r->width, 3u);
55 EXPECT_EQ(r->height, 3u);
56 EXPECT_EQ(r->alive.size(), 5u); // glider
57}
58
59TEST(CaIoExpected, RleErrorCarriesMessage)
60{
61 auto r = try_read_rle_string("this is not an RLE file @@@");
62 ASSERT_FALSE(r.has_value());
63 EXPECT_FALSE(r.error().empty());
64}
65
66TEST(CaIoExpected, RleIstreamOverload)
67{
68 std::istringstream in("x = 3, y = 1, rule = B3/S23\n3o!\n");
69 auto r = try_read_rle(in);
70 ASSERT_TRUE(r.has_value()) << (r.has_value() ? "" : r.error());
71 EXPECT_EQ(r->alive.size(), 3u);
72}
73
74TEST(CaIoExpected, RleEmptyInputFails)
75{
76 auto r = try_read_rle_string("");
77 ASSERT_FALSE(r.has_value());
78 EXPECT_FALSE(r.error().empty());
79}
80
81// --- Plaintext -------------------------------------------------------------
82
83TEST(CaIoExpected, PlaintextSuccessAndError)
84{
85 std::istringstream ok("!Blinker\nOOO\n");
86 auto a = try_read_plaintext(ok);
87 ASSERT_TRUE(a.has_value()) << (a.has_value() ? "" : a.error());
88 EXPECT_EQ(a->alive.size(), 3u);
89
90 std::istringstream bad("!Comment\nO@O\n"); // '@' is invalid
91 auto b = try_read_plaintext(bad);
92 ASSERT_FALSE(b.has_value());
93 EXPECT_FALSE(b.error().empty());
94}
95
96// --- Life ------------------------------------------------------------------
97
98TEST(CaIoExpected, Life105SuccessAndError)
99{
100 std::istringstream ok("#Life 1.05\n#P 0 0\n***\n");
101 auto a = try_read_life_105(ok);
102 ASSERT_TRUE(a.has_value()) << (a.has_value() ? "" : a.error());
103 EXPECT_EQ(a->alive.size(), 3u);
104
105 std::istringstream bad("garbage without a header\n");
106 auto b = try_read_life_105(bad);
107 ASSERT_FALSE(b.has_value());
108 EXPECT_FALSE(b.error().empty());
109}
110
111TEST(CaIoExpected, Life106SuccessAndError)
112{
113 std::istringstream ok("#Life 1.06\n0 0\n1 0\n2 0\n");
114 auto a = try_read_life_106(ok);
115 ASSERT_TRUE(a.has_value()) << (a.has_value() ? "" : a.error());
116 EXPECT_EQ(a->alive.size(), 3u);
117
118 std::istringstream bad("#Life 1.06\nnot two integers\n");
119 auto b = try_read_life_106(bad);
120 ASSERT_FALSE(b.has_value());
121 EXPECT_FALSE(b.error().empty());
122}
123
124TEST(CaIoExpected, LifeEmptyInputFails)
125{
126 std::istringstream empty105("");
127 auto a = try_read_life_105(empty105);
128 ASSERT_FALSE(a.has_value());
129 EXPECT_FALSE(a.error().empty());
130
131 std::istringstream empty106("");
132 auto b = try_read_life_106(empty106);
133 ASSERT_FALSE(b.has_value());
134 EXPECT_FALSE(b.error().empty());
135}
136
137// --- CSV -------------------------------------------------------------------
138
139TEST(CaIoExpected, CsvSuccessAndError)
140{
141 std::istringstream ok("0,1,0\n0,1,0\n0,1,0\n");
142 auto a = try_read_csv_snapshot<int>(ok);
143 ASSERT_TRUE(a.has_value()) << (a.has_value() ? "" : a.error());
144 EXPECT_EQ(a->height, 3u);
145 EXPECT_EQ(a->width, 3u);
146
147 std::istringstream ragged("0,1,0\n1,1\n"); // inconsistent width
148 auto b = try_read_csv_snapshot<int>(ragged);
149 ASSERT_FALSE(b.has_value());
150 EXPECT_FALSE(b.error().empty());
151}
152
153TEST(CaIoExpected, CsvEmptyInputHandled)
154{
155 std::istringstream empty("");
156 auto r = try_read_csv_snapshot<int>(empty);
157 ASSERT_TRUE(r.has_value()) << (r.has_value() ? "" : r.error());
158 EXPECT_EQ(r->height, 0u); // empty CSV is valid: 0 rows
159}
160
161// --- The throwing readers are unchanged ------------------------------------
162
163TEST(CaIoExpected, ThrowingReadersStillThrow)
164{
165 EXPECT_ANY_THROW((void) read_rle_string("not an RLE @@@"));
166
167 std::istringstream bad_plaintext("O@O\n"); // '@' invalid
168 EXPECT_ANY_THROW((void) read_plaintext(bad_plaintext));
169
170 std::istringstream bad_life105("garbage\n");
171 EXPECT_ANY_THROW((void) read_life_105(bad_life105));
172
173 std::istringstream bad_life106("#Life 1.06\nnot two integers\n");
174 EXPECT_ANY_THROW((void) read_life_106(bad_life106));
175
176 std::istringstream bad_csv("1,2,3\n1,2\n"); // ragged rows
177 EXPECT_ANY_THROW((void) read_csv_snapshot<int>(bad_csv));
178}
File-format readers and writers for cellular-automata frames.
#define TEST(name)
expected< Binary_Cell_Pattern, std::string > try_read_life_106(std::istream &in)
Non-throwing variant of read_life_106.
Definition ca-io.H:1841
expected< RLE_Pattern, std::string > try_read_rle_string(const std::string &s, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Non-throwing variant of read_rle_string.
Definition ca-io.H:1774
expected< Binary_Cell_Pattern, std::string > try_read_plaintext(std::istream &in)
Non-throwing variant of read_plaintext.
Definition ca-io.H:1797
Binary_Cell_Pattern read_life_105(std::istream &in)
Read a Life 1.05 pattern.
Definition ca-io.H:1170
expected< RLE_Pattern, std::string > try_read_rle(std::istream &in, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Non-throwing variant of read_rle.
Definition ca-io.H:1750
Binary_Cell_Pattern read_life_106(std::istream &in)
Read a Life 1.06 pattern.
Definition ca-io.H:1068
Binary_Cell_Pattern read_plaintext(std::istream &in)
Read a Life plaintext (.cells) pattern.
Definition ca-io.H:976
RLE_Pattern read_rle_string(const std::string &s, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Read a Conway RLE pattern from a string.
Definition ca-io.H:839
expected< Binary_Cell_Pattern, std::string > try_read_life_105(std::istream &in)
Non-throwing variant of read_life_105.
Definition ca-io.H:1819
Main namespace for Aleph-w library functions.
Gsl_Compatible_Rng r