structurereader_tests.cpp 2.84 KB
Newer Older
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
#include <QtTest/QtTest>

#include <algorithm>

#include "cellulut_tests.hpp"

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

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

const char* rle_glider_gun =
    "#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!";

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

const char* json_test_1 =
    "{\n"
    "    \"cells\" : [\n"
    "        { \"x\" : 1, \"y\" : 2, \"state\" : 1},\n"
    "        { \"x\" : 1, \"y\" : 3, \"state\" : 3}\n"
    "        ]\n"
    "}";

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

        try {
            Structure stru = rle.read_structure();
            QCOMPARE(stru.size(), 5);
            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
58
59
60
61
62
        }
    }

    {
        RLEStructureReader rle(rle_glider_gun);

        try {

            Structure stru = rle.read_structure();
            QCOMPARE(stru.size(), 36);
        }
        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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        }
    }

    {
        RLEStructureReader rle(rle_wireworld);

        try {

            Structure stru = rle.read_structure();
            QCOMPARE(stru.size(), 16);

            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
99
100
101
102
103
104
105
        }
    }
}

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

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