1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2006 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 // -----------------------------------------------------------------------
27
28 #include "libreallive/gameexe.h"
29
30 #include "gtest/gtest.h"
31
32 #include "test_utils.h"
33
34 #include <string>
35 #include <vector>
36
37 using namespace std;
38
TEST(GameexeUnit,ReadAllKeys)39 TEST(GameexeUnit, ReadAllKeys) {
40 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
41 EXPECT_EQ(26, ini.size()) << "Wrong number of keys";
42 }
43
44 // Make sure #CAPTION exists and that we read its value correctly.
TEST(GameexeUnit,ReadsCaption)45 TEST(GameexeUnit, ReadsCaption) {
46 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
47 EXPECT_TRUE(ini("CAPTION").Exists()) << "#CAPTION exists";
48 EXPECT_EQ(string("Canon: A firearm"), ini("CAPTION").ToString())
49 << "Wrong value for CAPTION";
50 }
51
52 // Make sure #RANDOM_KEY doesn't exist.
TEST(GameexeUnit,RandomKeyDoesntExist)53 TEST(GameexeUnit, RandomKeyDoesntExist) {
54 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
55 EXPECT_FALSE(ini("RANDOM_KEY").Exists()) << "#RANDOM_KEY does not exist";
56 }
57
58 // Test ToIntVector() parsing.
TEST(GameexeUnit,IntVectorParsing)59 TEST(GameexeUnit, IntVectorParsing) {
60 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
61 EXPECT_TRUE(ini("WINDOW_ATTR").Exists()) << "#WINDOW_ATTR exists!";
62
63 vector<int> ints = ini("WINDOW_ATTR").ToIntVector();
64 for (int i = 0; i < 5; ++i) {
65 EXPECT_EQ(i + 1, ints.at(i));
66 }
67 }
68
69 // Make sure operator() works with multiple keys...
TEST(GameexeUnit,MultipleKeys)70 TEST(GameexeUnit, MultipleKeys) {
71 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
72 EXPECT_EQ(1, ini("IMAGINE", "ONE"));
73 EXPECT_EQ(2, ini("IMAGINE", "TWO"));
74 EXPECT_EQ(3, ini("IMAGINE", "THREE"));
75 }
76
77 // Make sure GameexeInterpretObject chaining works correctly.
TEST(GameexeUnit,ChainingWorks)78 TEST(GameexeUnit, ChainingWorks) {
79 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
80 GameexeInterpretObject imagine = ini("IMAGINE");
81 EXPECT_EQ(1, imagine("ONE"));
82 EXPECT_EQ(2, imagine("TWO"));
83 EXPECT_EQ(3, imagine("THREE"));
84 }
85
TEST(GameexeUnit,FilteringIterators)86 TEST(GameexeUnit, FilteringIterators) {
87 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
88 GameexeFilteringIterator it = ini.filtering_begin("IMAGINE");
89 GameexeFilteringIterator end = ini.filtering_end();
90 for (; it != end; ++it) {
91 if (it->key() != "IMAGINE.ONE" && it->key() != "IMAGINE.TWO" &&
92 it->key() != "IMAGINE.THREE") {
93 FAIL() << "Failed to filter keys in GameexeFilteringIterator. Has key "
94 << it->key();
95 }
96 }
97 }
98
TEST(GameexeUnit,KeyParts)99 TEST(GameexeUnit, KeyParts) {
100 Gameexe ini(locateTestCase("Gameexe_data/Gameexe.ini"));
101 GameexeInterpretObject gio = ini("WINDOW.000.ATTR_MOD");
102 vector<string> pieces = gio.GetKeyParts();
103 EXPECT_EQ(3, pieces.size());
104 EXPECT_EQ("WINDOW", pieces[0]);
105 EXPECT_EQ("000", pieces[1]);
106 EXPECT_EQ("ATTR_MOD", pieces[2]);
107 }
108
109 // A demo on the Princess Bride Box Set does something weird with its \#DSTRACK
110 // keys. While all other games space the entries like this:
111 //
112 // #DSTRACK = 00000000 - 99999999 - 00269364 = "BGM01" = "BGM01"
113 //
114 // this game does not:
115 //
116 // #DSTRACK=00000000-10998934-00000000="dcbgm000"="dcbgm000"
117 //
118 // and this runs afoul of the gameexe tokenization
119 // code. (00000000-10998934-00000000 is treated as a single token.)
120 //
121 // This test ensures that the gameexe parser can handle both.
TEST(GameexeUnit,DstrackRegression)122 TEST(GameexeUnit, DstrackRegression) {
123 Gameexe ini(locateTestCase("Gameexe_data/Gameexe_tokenization.ini"));
124
125 GameexeInterpretObject clannad = ini("CLANNADDSTRACK");
126 EXPECT_EQ(0, clannad.GetIntAt(0));
127 EXPECT_EQ(99999999, clannad.GetIntAt(1));
128 EXPECT_EQ(269364, clannad.GetIntAt(2));
129 EXPECT_EQ("BGM01", clannad.GetStringAt(3));
130 EXPECT_EQ("BGM01", clannad.GetStringAt(4));
131
132 GameexeInterpretObject dc = ini("DCDSTRACK");
133 EXPECT_EQ(0, dc.GetIntAt(0));
134 EXPECT_EQ(10998934, dc.GetIntAt(1));
135 EXPECT_EQ(0, dc.GetIntAt(2));
136 EXPECT_EQ("dcbgm000", dc.GetStringAt(3));
137 EXPECT_EQ("dcbgm000", dc.GetStringAt(4));
138 }
139