structurereader_tests.cpp 2.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
#include <QtTest/QtTest>

#include <algorithm>

#include "cellulut_tests.hpp"

#include "structure.hpp"
#include "structurereader.hpp"

10
static const char* rle_glider =
11
12
13
14
    "#C This is a glider.\n"
    "x = 3, y = 3\n"
    "bo$2bo$3o!\n";

15
static const char* rle_glider_gun =
16
17
18
19
20
21
22
    "#N Gosper glider gun\n"
    "#C This was the first gun discovered.\n"
    "#C As its name suggests, it was discovered by Bill Gosper.\n"
    "    x = 36, y = 9, rule = B3/S23\n"
    "24bo$22bobo$12b2o6b2o12b2o$11bo3bo4b2o12b2o$2o8bo5bo3b2o$2o8bo3bob2o4b\n"
    "obo$10bo5bo7bo$11bo3bo$12b2o!";

23
static const char* rle_wireworld =
24
25
26
    "x = 6, y = 7, rule = WireWorld\n"
    ".A$.C$4C$C2.3C$4C$.C$.A!";

27
static const char* json_test_1 =
28
29
    "{\n"
    "    \"cells\" : [\n"
30
31
    "        { \"x\" : 0, \"y\" : 0, \"state\" : 1},\n"
    "        { \"x\" : 2, \"y\" : 3, \"state\" : 3}\n"
32
33
34
35
36
37
38
39
40
41
    "        ]\n"
    "}";

void CellulutTests::test_rle_structurereader()
{
    {
        RLEStructureReader rle(rle_glider);

        try {
            Structure stru = rle.read_structure();
42
            QCOMPARE(stru.size(), 5u);
43
44
45
46
47
            QVERIFY(std::count(stru.begin(), stru.end(), std::make_pair<Coord, unsigned>(Coord{1, 0}, 1)) == 1);
        }
        catch (const std::exception& ex)
        {
            fprintf(stderr, "Error is %s\n", ex.what());
48
            QFAIL("Exception thrown");
49
50
51
52
53
54
55
56
57
        }
    }

    {
        RLEStructureReader rle(rle_glider_gun);

        try {

            Structure stru = rle.read_structure();
58
            QCOMPARE(stru.size(), 36u);
59
60
61
62
        }
        catch (const std::exception& ex)
        {
            fprintf(stderr, "Error is %s\n", ex.what());
63
            QFAIL("Exception thrown");
64
65
66
67
68
69
70
71
72
        }
    }

    {
        RLEStructureReader rle(rle_wireworld);

        try {

            Structure stru = rle.read_structure();
73
            QCOMPARE(stru.size(), 16u);
74
75
76
77
78
79
80
81
82
83
84
85
86

            std::map<int, int> state_count;
            for (auto pair : stru)
            {
                state_count[pair.second]++;
            }
            QCOMPARE(state_count[1], 2);
            QCOMPARE(state_count[2], 0);
            QCOMPARE(state_count[3], 14);
        }
        catch (const std::exception& ex)
        {
            fprintf(stderr, "Error is %s\n", ex.what());
87
            QFAIL("Exception thrown");
88
89
90
91
92
93
94
95
96
97
98
        }
    }
}

void CellulutTests::test_json_structurereader()
{
    {
        JSONStructureReader rle(json_test_1);

        try {
            Structure stru = rle.read_structure();
99
            QCOMPARE(stru.size(), 2u);
100
101
            QVERIFY(std::count(stru.begin(), stru.end(), std::make_pair<Coord, unsigned>(Coord{0, 0}, 1)) == 1);
            QVERIFY(std::count(stru.begin(), stru.end(), std::make_pair<Coord, unsigned>(Coord{2, 3}, 3)) == 1);
102
103
104
105
        }
        catch (const std::exception& ex)
        {
            fprintf(stderr, "Error is %s\n", ex.what());
106
            QFAIL("Exception thrown");
107
108
109
        }
    }
}