Aleph-w 3.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
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
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
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
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
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
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
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
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
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
149 ASSERT_FALSE(b.has_value());
150 EXPECT_FALSE(b.error().empty());
151}
152
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
164{
165 EXPECT_ANY_THROW((void) read_rle_string("not an RLE @@@"));
166
167 std::istringstream bad_plaintext("O@O\n"); // '@' invalid
169
170 std::istringstream bad_life105("garbage\n");
172
173 std::istringstream bad_life106("#Life 1.06\nnot two integers\n");
175
176 std::istringstream bad_csv("1,2,3\n1,2\n"); // ragged rows
178}
File-format readers and writers for cellular-automata frames.
#define TEST(name)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
expected< Binary_Cell_Pattern, std::string > try_read_life_106(std::istream &in)
Non-throwing variant of read_life_106.
Definition ca-io.H:1768
expected< Binary_Cell_Pattern, std::string > try_read_plaintext(std::istream &in)
Non-throwing variant of read_plaintext.
Definition ca-io.H:1724
Binary_Cell_Pattern read_life_105(std::istream &in)
Read a Life 1.05 pattern.
Definition ca-io.H:1106
RLE_Pattern read_rle_string(const std::string &s)
Read a Conway RLE pattern from a string.
Definition ca-io.H:785
Binary_Cell_Pattern read_life_106(std::istream &in)
Read a Life 1.06 pattern.
Definition ca-io.H:1007
Binary_Cell_Pattern read_plaintext(std::istream &in)
Read a Life plaintext (.cells) pattern.
Definition ca-io.H:919
expected< RLE_Pattern, std::string > try_read_rle_string(const std::string &s)
Non-throwing variant of read_rle_string.
Definition ca-io.H:1702
expected< RLE_Pattern, std::string > try_read_rle(std::istream &in)
Non-throwing variant of read_rle.
Definition ca-io.H:1680
expected< Binary_Cell_Pattern, std::string > try_read_life_105(std::istream &in)
Non-throwing variant of read_life_105.
Definition ca-io.H:1746
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
gsl_rng * r