check_lexer.cpp 2.83 KB
Newer Older
Albert Astals Cid's avatar
Albert Astals Cid committed
1 2 3 4 5 6 7 8
#include <QtTest/QtTest>

#include "Object.h"
#include "Lexer.h"

class TestLexer : public QObject
{
    Q_OBJECT
9 10
public:
    TestLexer(QObject *parent = nullptr) : QObject(parent) { }
Albert Astals Cid's avatar
Albert Astals Cid committed
11 12 13 14 15 16
private slots:
    void testNumbers();
};

void TestLexer::testNumbers()
{
17
    char data[] = "0 1 -1 2147483647 -2147483647 2147483648 -2147483648 4294967297 -2147483649 0.1 1.1 -1.1 2147483647.1 -2147483647.1 2147483648.1 -2147483648.1 4294967297.1 -2147483649.1 9223372036854775807 18446744073709551615";
Albert Astals Cid's avatar
Albert Astals Cid committed
18
    MemStream *stream = new MemStream(data, 0, strlen(data), Object(objNull));
19
    Lexer *lexer = new Lexer(nullptr, stream);
Albert Astals Cid's avatar
Albert Astals Cid committed
20 21 22 23
    QVERIFY( lexer );
    
    Object obj;
    
Albert Astals Cid's avatar
Albert Astals Cid committed
24
    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
25 26
    QCOMPARE(obj.getType(), objInt);
    QCOMPARE(obj.getInt(), 0);
Albert Astals Cid's avatar
Albert Astals Cid committed
27 28

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
29 30
    QCOMPARE(obj.getType(), objInt);
    QCOMPARE(obj.getInt(), 1);
Albert Astals Cid's avatar
Albert Astals Cid committed
31 32

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
33 34
    QCOMPARE(obj.getType(), objInt);
    QCOMPARE(obj.getInt(), -1);
Albert Astals Cid's avatar
Albert Astals Cid committed
35 36

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
37 38
    QCOMPARE(obj.getType(), objInt);
    QCOMPARE(obj.getInt(), 2147483647);
Albert Astals Cid's avatar
Albert Astals Cid committed
39 40

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
41 42
    QCOMPARE(obj.getType(), objInt);
    QCOMPARE(obj.getInt(), -2147483647);
Albert Astals Cid's avatar
Albert Astals Cid committed
43 44

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
45 46 47
    QCOMPARE(obj.getType(), objInt64);
    QCOMPARE(obj.getInt64(), 2147483648ll);
      
Albert Astals Cid's avatar
Albert Astals Cid committed
48
    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
49 50
    QCOMPARE(obj.getType(), objInt);
    QCOMPARE(obj.getInt(), -2147483647-1);
Albert Astals Cid's avatar
Albert Astals Cid committed
51 52

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
53 54
    QCOMPARE(obj.getType(), objInt64);
    QCOMPARE(obj.getInt64(), 4294967297ll);
Albert Astals Cid's avatar
Albert Astals Cid committed
55 56

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
57 58 59
    QCOMPARE(obj.getType(), objInt64);
    QCOMPARE(obj.getInt64(), -2147483649ll);

Albert Astals Cid's avatar
Albert Astals Cid committed
60
    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
61 62
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), 0.1);
Albert Astals Cid's avatar
Albert Astals Cid committed
63 64

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
65 66
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), 1.1);
Albert Astals Cid's avatar
Albert Astals Cid committed
67 68

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
69 70
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), -1.1);
Albert Astals Cid's avatar
Albert Astals Cid committed
71 72

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
73 74
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), 2147483647.1);
Albert Astals Cid's avatar
Albert Astals Cid committed
75 76

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
77 78
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), -2147483647.1);
Albert Astals Cid's avatar
Albert Astals Cid committed
79 80

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
81 82 83
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), 2147483648.1);
      
Albert Astals Cid's avatar
Albert Astals Cid committed
84
    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
85 86
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), -2147483648.1);
Albert Astals Cid's avatar
Albert Astals Cid committed
87 88

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
89 90
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), 4294967297.1);
Albert Astals Cid's avatar
Albert Astals Cid committed
91 92

    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
93 94 95
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), -2147483649.1);

Albert Astals Cid's avatar
Albert Astals Cid committed
96
    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
97 98 99
    QCOMPARE(obj.getType(), objInt64);
    QCOMPARE(obj.getInt64(), 9223372036854775807ll);

Albert Astals Cid's avatar
Albert Astals Cid committed
100
    obj = lexer->getObj();
Albert Astals Cid's avatar
Albert Astals Cid committed
101 102 103 104 105 106
    QCOMPARE(obj.getType(), objReal);
    QCOMPARE(obj.getReal(), 18446744073709551616.);

    delete lexer;
}

107
QTEST_GUILESS_MAIN(TestLexer)
Albert Astals Cid's avatar
Albert Astals Cid committed
108 109
#include "check_lexer.moc"