Skip to content

Commit

Permalink
Merge pull request #323 from tobiatesan/test_for_common
Browse files Browse the repository at this point in the history
Test for common
  • Loading branch information
lordhoto committed Jul 15, 2013
2 parents 6a1112f + d2c85e1 commit 3e246b3
Show file tree
Hide file tree
Showing 4 changed files with 629 additions and 0 deletions.
164 changes: 164 additions & 0 deletions test/common/hash-str.h
@@ -0,0 +1,164 @@
#include <cxxtest/TestSuite.h>
#include "common/hash-str.h"

/**
* Test suite for common/hash-str.h
* We test a number of case sensitive/insensitive hash and compare functions
* using example strings and known hashes, trying to tackle
* as much edge cases as possible.
*/
class HashStrTestSuite : public CxxTest::TestSuite {

public:
void test_case_sensitive_string_equal_to() {

// Name says it all.
// This verifies that the function returns true
// for exactly the same string, false for the same
// string in mixed case and false for some edge cases
// with various spacings plus one character replaced
// by itself+128 (if there's some processing done after
// conversion to 7-bit ASCII this might yield funny results).

const Common::String lower("test");
const Common::String lower1("test");
const Common::String mixed("tESt");
const Common::String spaced("test ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");
const Common::String plus128("t\345est");
// 'e'+128 = 0xE5 = 0o345

Common::CaseSensitiveString_EqualTo css_et;
TS_ASSERT_EQUALS(css_et(lower, mixed), false);
TS_ASSERT_EQUALS(css_et(lower, lower1), true);
TS_ASSERT_EQUALS(css_et(lower, lower), true);

// Different sorts of whitespace are to be treated differently.
TS_ASSERT_EQUALS(css_et(lower, spaced), false);
TS_ASSERT_EQUALS(css_et(lower, tabbed), false);
TS_ASSERT_EQUALS(css_et(spaced, tabbed), false);
TS_ASSERT_EQUALS(css_et(spaced, doublespaced), false);
TS_ASSERT_EQUALS(css_et(lower, plus128), false);
}

void test_ignore_case_equal_to() {

// This should be probably called case_insensitive_string_equal_to
// or something,but it's basically the same thing as
// test_case_sensitive_string_equal_to, only it's case
// insensitive.

const Common::String lower("test");
const Common::String lower1("test");
const Common::String mixed("tESt");
const Common::String spaced("test ");
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");
const Common::String plus128("t\345est");

Common::IgnoreCase_EqualTo ic_et;
TS_ASSERT_EQUALS(ic_et(lower, mixed), true);
TS_ASSERT_EQUALS(ic_et(lower, lower1), true);
TS_ASSERT_EQUALS(ic_et(lower, lower), true);
// Edge case:
TS_ASSERT_EQUALS(ic_et(spaced, mixedspaced), true);

// Different sorts of whitespace are to be treated differently.
TS_ASSERT_EQUALS(ic_et(lower, spaced), false);
TS_ASSERT_EQUALS(ic_et(lower, tabbed), false);
TS_ASSERT_EQUALS(ic_et(spaced, tabbed), false);
TS_ASSERT_EQUALS(ic_et(spaced, doublespaced), false);
TS_ASSERT_EQUALS(ic_et(lower, plus128), false);
}

void test_case_sensitive_string_hash() {

// Here we compute string hashes for different
// strings and see that the functor is case sensitive
// and does not ignore spaces.

const Common::String lower("test");
const Common::String lower1("test");
const Common::String mixed("tESt");
const Common::String spaced("test ");
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");

Common::CaseSensitiveString_Hash css_h;
TS_ASSERT_EQUALS(css_h(lower), css_h(lower1));
TS_ASSERT_DIFFERS(css_h(mixed), css_h(lower));
TS_ASSERT_DIFFERS(css_h(spaced), css_h(lower));
TS_ASSERT_DIFFERS(css_h(tabbed), css_h(spaced));
TS_ASSERT_DIFFERS(css_h(spaced), css_h(doublespaced));
}

void test_ignore_case_hash() {
// Same as test_case_sensitive_string_hash, but case insensitive.
const Common::String lower("test");
const Common::String lower1("test");
const Common::String mixed("tESt");
const Common::String spaced("test ");
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");

Common::IgnoreCase_Hash ic_h;
TS_ASSERT_EQUALS(ic_h(lower), ic_h(lower1));
TS_ASSERT_EQUALS(ic_h(mixed), ic_h(lower));
TS_ASSERT_EQUALS(ic_h(spaced), ic_h(mixedspaced));
TS_ASSERT_DIFFERS(ic_h(tabbed), ic_h(lower));
TS_ASSERT_DIFFERS(ic_h(spaced), ic_h(doublespaced));
}

void test_cpp_string_hash()
{
// We run the same tests with Hash<String>,
// a template specialization of Hash, also a functor.
// It is supposed to be case sensitive.

const Common::String lower("test");
const Common::String lower1("test");
const Common::String mixed("tESt");
const Common::String spaced("test ");
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");

Common::Hash<Common::String> h;
TS_ASSERT_EQUALS(h(lower), h(lower1));
TS_ASSERT_DIFFERS(h(mixed), h(lower));
TS_ASSERT_DIFFERS(h(spaced), h(lower));
TS_ASSERT_DIFFERS(h(tabbed), h(spaced));
TS_ASSERT_DIFFERS(h(spaced), h(doublespaced));
}

void test_c_style_string_hash()
{
// Same as test_cpp_string_hash but with Hash<const char*>,
// a template specialization of Hash, also a functor,
// that works with C-Style strings.
// It is supposed to be case sensitive.

char lower[] = "test";
char lower1[] = "test";
char mixed[] = "tESt";
char spaced[] = "test ";
char mixedspaced[] = "tESt ";
char doublespaced[] = "test ";
char tabbed[] = "test\t";

Common::Hash<const char *> h;
TS_ASSERT_EQUALS(h(lower), h(lower1));
TS_ASSERT_DIFFERS(h(mixed), h(lower));
TS_ASSERT_DIFFERS(h(spaced), h(lower));
TS_ASSERT_DIFFERS(h(spaced), h(mixedspaced));
TS_ASSERT_DIFFERS(h(tabbed), h(spaced));
TS_ASSERT_DIFFERS(h(spaced), h(doublespaced));

}


};
144 changes: 144 additions & 0 deletions test/common/huffman.h
@@ -0,0 +1,144 @@
#include <cxxtest/TestSuite.h>
#include "common/huffman.h"
#include "common/bitstream.h"
#include "common/memstream.h"

/**
* A test suite for the Huffman decoder in common/huffman.h
* The encoding used comes from the example on the Wikipedia page
* for Huffman.
* TODO: It could be improved by generating one at runtime.
*/
class HuffmanTestSuite : public CxxTest::TestSuite {
public:
void test_get_with_full_symbols() {

/*
* The class can be initialized with or without providing
* a max_length and a symbol table.
* We test with a table.
*
* Encoding (arbitrary, for testing purpouses):
* 0xA=010
* 0xB=011
* 0xC=11
* 0xD=00
* 0xE=10
*/

uint32 codeCount = 5;
uint8 maxLength = 3;
const uint8 lengths[] = {3,3,2,2,2};
const uint32 codes[] = {0x2, 0x3, 0x3, 0x0, 0x2};
const uint32 symbols[] = {0xA, 0xB, 0xC, 0xD, 0xE};

Common::Huffman h(maxLength, codeCount, codes, lengths, symbols);

byte input[] = {0x4F, 0x20};
// Provided input...
uint32 expected[] = {0xA, 0xB, 0xC, 0xD, 0xE, 0xD, 0xD};
// ..and expected output.

/*
* What should be going on:
* 010 011 11 00 10 00 00 = A B C D E D D
* = 0100 1111 0010 0000 = 0x4F20
*/

Common::MemoryReadStream ms(input, sizeof(input));
Common::BitStream8MSB bs(ms);

TS_ASSERT_EQUALS(h.getSymbol(bs), expected[0]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[1]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[2]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[3]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[4]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[5]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[6]);
}

void test_get_without_symbols() {

/*
* This is basically the same as test_get_with_full_symbols, but
* I only pass the minimal required arguments.
* Specifically, I avoid passing the symbols table, so that
* array indices are used instead.
*
* Encoding becomes:
*
* 0=010
* 1=011
* 2=11
* 3=00
* 4=10
*/

uint32 codeCount = 5;
const uint8 lengths[] = {3,3,2,2,2};
const uint32 codes[] = {0x2, 0x3, 0x3, 0x0, 0x2};

Common::Huffman h(0, codeCount, codes, lengths, 0);

byte input[] = {0x4F, 0x20};
uint32 expected[] = {0, 1, 2, 3, 4, 3 ,3};

Common::MemoryReadStream ms(input, sizeof(input));
Common::BitStream8MSB bs(ms);

TS_ASSERT_EQUALS(h.getSymbol(bs), expected[0]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[1]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[2]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[3]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[4]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[5]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[6]);
}

void test_get_after_set_symbols() {

/*
* Another variation of test_get_with_full_symbols.
* I use the setSymbols method to define, a posteriori,
* an alphabet to be used in place of array indices.
* The encoding is, at first,
* 0=010
* 1=011
* 2=11
* 3=00
* 4=10
* (=array indices).
*/

uint32 codeCount = 5;
const uint8 lengths[] = {3,3,2,2,2};
const uint32 codes[] = {0x2, 0x3, 0x3, 0x0, 0x2};

Common::Huffman h(0, codeCount, codes, lengths, 0);

const uint32 symbols[] = {0xA, 0xB, 0xC, 0xD, 0xE};
h.setSymbols(symbols);

byte input[] = {0x4F, 0x20};
uint32 expected[] = {0xA, 0xB, 0xC, 0xD, 0xE, 0xD, 0xD};

Common::MemoryReadStream ms(input, sizeof(input));
Common::BitStream8MSB bs(ms);

/* New symbols:
* A=010
* B=011
* C=11
* D=00
* E=10
*/

TS_ASSERT_EQUALS(h.getSymbol(bs), expected[0]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[1]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[2]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[3]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[4]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[5]);
TS_ASSERT_EQUALS(h.getSymbol(bs), expected[6]);
}
};
84 changes: 84 additions & 0 deletions test/common/rendermode.h
@@ -0,0 +1,84 @@
#include <cxxtest/TestSuite.h>
#include "common/rendermode.h"
#include "common/gui_options.h"
#include "common/str.h"

class RenderModeTestSuite : public CxxTest::TestSuite {
public:
void test_parse_render_mode_good() {
/*
* Tests for parseRenderMode.
* It takes a code (case-insensitive) and spits a RenderMode back at you.
* These cases should work - the inputs are standard, there's just some
* fun with caps being had in here.
*/
TS_ASSERT_EQUALS(Common::parseRenderMode("fMTOwNs"), Common::kRenderFMTowns);
TS_ASSERT_EQUALS(Common::parseRenderMode("hercGrEen"), Common::kRenderHercG);
TS_ASSERT_EQUALS(Common::parseRenderMode("hercAmbeR"), Common::kRenderHercA);
TS_ASSERT_EQUALS(Common::parseRenderMode("CgA"), Common::kRenderCGA);
TS_ASSERT_EQUALS(Common::parseRenderMode("ega"), Common::kRenderEGA);
TS_ASSERT_EQUALS(Common::parseRenderMode("Vga"), Common::kRenderVGA);
TS_ASSERT_EQUALS(Common::parseRenderMode("AmigA"), Common::kRenderAmiga);
TS_ASSERT_EQUALS(Common::parseRenderMode("pc9821"), Common::kRenderPC9821);
TS_ASSERT_EQUALS(Common::parseRenderMode("PC9801"), Common::kRenderPC9801);
TS_ASSERT_EQUALS(Common::parseRenderMode("0"), Common::kRenderDefault);
}


void test_parse_render_mode_bad() {
/*
* These cases, according to the specification, should return the default.
* It is only mentioned that the function must be case insensitive.
* Whitespaces, in particular, should not be automatically trimmed.
*/
TS_ASSERT_EQUALS(Common::parseRenderMode("fmtowns "), Common::kRenderDefault);
TS_ASSERT_EQUALS(Common::parseRenderMode("FM-TOWNS "), Common::kRenderDefault);
TS_ASSERT_EQUALS(Common::parseRenderMode(" cga"), Common::kRenderDefault);
TS_ASSERT_EQUALS(Common::parseRenderMode("\tC g A"), Common::kRenderDefault);
TS_ASSERT_EQUALS(Common::parseRenderMode("\t"), Common::kRenderDefault);
// This is the only interesting bit: if the function was really, really
// broken it could be tempted to test for +-0x20.
TS_ASSERT_EQUALS(Common::parseRenderMode("pc Y8 21 "), Common::kRenderDefault);
TS_ASSERT_EQUALS(Common::parseRenderMode(" PC\t9801 "), Common::kRenderDefault);
TS_ASSERT_EQUALS(Common::parseRenderMode("0"), Common::kRenderDefault);
}

void test_get_render_mode_code_back_and_forth() {
/*
* What does getRenderModeCode return?
* Notably, the output should not be in mixed case.
*/
TS_ASSERT_SAME_DATA(Common::getRenderModeCode(Common::parseRenderMode("FMTOWNS")), "fmtowns", 7);
TS_ASSERT_DIFFERS(Common::getRenderModeCode(Common::parseRenderMode("FMTOWNS")), "fmtowns");
TS_ASSERT_SAME_DATA(Common::getRenderModeCode(Common::parseRenderMode("CGA")), "cga", 3);
TS_ASSERT_SAME_DATA(Common::getRenderModeCode(Common::parseRenderMode("vga")), "vga", 3);
TS_ASSERT_SAME_DATA(Common::getRenderModeCode(Common::parseRenderMode("Ega")), "ega", 3);
TS_ASSERT_SAME_DATA(Common::getRenderModeCode(Common::parseRenderMode("AmiGa")), "amiga", 5);
TS_ASSERT_SAME_DATA(Common::getRenderModeCode(Common::parseRenderMode("PC9821")), "pc9821", 6);
TS_ASSERT_SAME_DATA(Common::getRenderModeCode(Common::parseRenderMode("PC9801")), "pc9801", 6);
// Slightly more interesting:
// Make sure that we get a null pointer for 0 (and not the "0" string or stuff)
char *null_p = 0;
TS_ASSERT_EQUALS(Common::getRenderModeCode(Common::kRenderDefault), null_p);
}

void test_render_2_guio() {
/*
* Verify that a rendermode is taken and the corresponding
* GUIO_xxxxx is returned.
*/
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderHercG), GUIO_RENDERHERCGREEN);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderHercA), GUIO_RENDERHERCAMBER);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderCGA), GUIO_RENDERCGA);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderEGA), GUIO_RENDEREGA);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderVGA), GUIO_RENDERVGA);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderAmiga), GUIO_RENDERAMIGA);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderFMTowns), GUIO_RENDERFMTOWNS);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderPC9821), GUIO_RENDERPC9821);
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderPC9801), GUIO_RENDERPC9801);
// renderMode2GUIO is supposed to return an empty string
// if given kRenderDefault as an argument
Common::String empty;
TS_ASSERT_EQUALS(Common::renderMode2GUIO(Common::kRenderDefault), empty);
}
};

0 comments on commit 3e246b3

Please sign in to comment.