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

#include <algorithm>

#include "cellulut_tests.hpp"

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

11
12
13
14
15
16
17
18
19
static const char* rle_glider =
    "x = 3, y = 3\n"
    "bo$2bo$3o!";

static const char* rle_glider_gun =
    "x = 36, y = 9\n"
    "24bo$22bobo$12b2o6b2o12b2o$11bo3bo4b2o12b2o$2o8bo5bo3b2o$2o8bo3bob2o4b"
    "obo$10bo5bo7bo$11bo3bo$12b2o!";

20
21
void CellulutTests::test_rle_structurewriter()
{
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
    try
    {
        Structure stru;

        RLEStructureReader reader(rle_glider);
        stru = reader.read_structure();

        RLEStructureWriter writer;
        auto out = writer.save_structure(stru);

        QCOMPARE(out, std::string(rle_glider));
    }
    catch (const std::exception& ex)
    {
        fprintf(stderr, "Error is %s\n", ex.what());
        QFAIL("Exception thrown");
    }

    try
    {
        Structure stru;

        RLEStructureReader reader(rle_glider_gun);
        stru = reader.read_structure();

        RLEStructureWriter writer;
        auto out = writer.save_structure(stru);

        QCOMPARE(out, std::string(rle_glider_gun));
    }
    catch (const std::exception& ex)
    {
        fprintf(stderr, "Error is %s\n", ex.what());
        QFAIL("Exception thrown");
    }
57
58
59
60
61
}

void CellulutTests::test_json_structurewriter()
{
    std::vector<std::pair<Coord, unsigned>> coords;
62
63
64
    coords.push_back({{0, 0}, 3});
    coords.push_back({{1, 2}, 1});
    coords.push_back({{5, 8}, 5});
65

66
    try
67
    {
68
69
70
71
72
73
        Structure s{coords.begin(), coords.end()};
        s.author = "Bob";
        s.title = "Foo";
        s.date = "28/07/2001";
        s.desc = "Lorem ipsum";

74
        JSONStructureWriter writer;
75
        auto out = writer.save_structure(s);
76
77
78
79
80
81
82

        //printf("%s\n", out.c_str());

        JSONStructureReader reader(out);
        Structure struct_out = reader.read_structure();

        QVERIFY(std::is_permutation(struct_out.begin(), struct_out.end(), coords.begin()));
83
84
85
86
        QCOMPARE(struct_out.author, s.author);
        QCOMPARE(struct_out.date, s.date);
        QCOMPARE(struct_out.desc, s.desc);
        QCOMPARE(struct_out.title, s.title);
87
    }
88
89
90
91
92
    catch (const std::exception& ex)
    {
        fprintf(stderr, "Error is %s\n", ex.what());
        QFAIL("Exception thrown");
    }
93
}