X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Ftest.cpp;h=072bda8ef79ab63bd5abae70662328667918db68;hb=00fc0babe0c6c5464fa9ffbc5a257b1e2aa93111;hp=e609fe26f6c7e6ef705c8623ccc1ea44cc8aa3b4;hpb=967121a34bbc60e6b46c7ec470b151f668ef1fef;p=oweals%2Fminetest.git diff --git a/src/test.cpp b/src/test.cpp index e609fe26f..072bda8ef 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -70,20 +70,18 @@ with this program; if not, write to the Free Software Foundation, Inc., A few item and node definitions for those tests that need them */ -#define CONTENT_STONE 0 -#define CONTENT_GRASS 0x800 -#define CONTENT_TORCH 100 +static content_t CONTENT_STONE; +static content_t CONTENT_GRASS; +static content_t CONTENT_TORCH; void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef) { - content_t i; ItemDefinition itemdef; ContentFeatures f; /* Stone */ - i = CONTENT_STONE; itemdef = ItemDefinition(); itemdef.type = ITEM_NODE; itemdef.name = "default:stone"; @@ -99,12 +97,11 @@ void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *n f.tiledef[i].name = "default_stone.png"; f.is_ground_content = true; idef->registerItem(itemdef); - ndef->set(i, f); + CONTENT_STONE = ndef->set(f.name, f); /* Grass */ - i = CONTENT_GRASS; itemdef = ItemDefinition(); itemdef.type = ITEM_NODE; itemdef.name = "default:dirt_with_grass"; @@ -122,12 +119,11 @@ void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *n f.tiledef[i].name = "default_dirt.png^default_grass_side.png"; f.is_ground_content = true; idef->registerItem(itemdef); - ndef->set(i, f); + CONTENT_GRASS = ndef->set(f.name, f); /* Torch (minimal definition for lighting tests) */ - i = CONTENT_TORCH; itemdef = ItemDefinition(); itemdef.type = ITEM_NODE; itemdef.name = "default:torch"; @@ -138,7 +134,7 @@ void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *n f.sunlight_propagates = true; f.light_source = LIGHT_MAX-1; idef->registerItem(itemdef); - ndef->set(i, f); + CONTENT_TORCH = ndef->set(f.name, f); } struct TestBase @@ -161,14 +157,58 @@ struct TestUtilities: public TestBase UASSERT(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001); UASSERT(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001); UASSERT(lowercase("Foo bAR") == "foo bar"); + UASSERT(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR"); + UASSERT(trim("\n \t\r \r\n\t\t ") == ""); UASSERT(is_yes("YeS") == true); UASSERT(is_yes("") == false); UASSERT(is_yes("FAlse") == false); - const char *ends[] = {"abc", "c", "bc", NULL}; + UASSERT(is_yes("-1") == true); + UASSERT(is_yes("0") == false); + UASSERT(is_yes("1") == true); + UASSERT(is_yes("2") == true); + const char *ends[] = {"abc", "c", "bc", "", NULL}; UASSERT(removeStringEnd("abc", ends) == ""); UASSERT(removeStringEnd("bc", ends) == "b"); UASSERT(removeStringEnd("12c", ends) == "12"); UASSERT(removeStringEnd("foo", ends) == ""); + UASSERT(urlencode("\"Aardvarks lurk, OK?\"") + == "%22Aardvarks%20lurk%2C%20OK%3F%22"); + UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22") + == "\"Aardvarks lurk, OK?\""); + UASSERT(padStringRight("hello", 8) == "hello "); + UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc"))); + UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true)); + UASSERT(trim(" a") == "a"); + UASSERT(trim(" a ") == "a"); + UASSERT(trim("a ") == "a"); + UASSERT(trim("") == ""); + UASSERT(mystoi("123", 0, 1000) == 123); + UASSERT(mystoi("123", 0, 10) == 10); + std::string test_str; + test_str = "Hello there"; + str_replace(test_str, "there", "world"); + UASSERT(test_str == "Hello world"); + test_str = "ThisAisAaAtest"; + str_replace_char(test_str, 'A', ' '); + UASSERT(test_str == "This is a test"); + UASSERT(string_allowed("hello", "abcdefghijklmno") == true); + UASSERT(string_allowed("123", "abcdefghijklmno") == false); + UASSERT(string_allowed_blacklist("hello", "123") == true); + UASSERT(string_allowed_blacklist("hello123", "123") == false); + UASSERT(wrap_rows("12345678",4) == "1234\n5678"); + UASSERT(is_number("123") == true); + UASSERT(is_number("") == false); + UASSERT(is_number("123a") == false); + UASSERT(is_power_of_two(0) == false); + UASSERT(is_power_of_two(1) == true); + UASSERT(is_power_of_two(2) == true); + UASSERT(is_power_of_two(3) == false); + for (int exponent = 2; exponent <= 31; ++exponent) { + UASSERT(is_power_of_two((1 << exponent) - 1) == false); + UASSERT(is_power_of_two((1 << exponent)) == true); + UASSERT(is_power_of_two((1 << exponent) + 1) == false); + } + UASSERT(is_power_of_two((u32)-1) == false); } }; @@ -378,27 +418,94 @@ struct TestPath: public TestBase } }; +#define TEST_CONFIG_TEXT_BEFORE \ + "leet = 1337\n" \ + "leetleet = 13371337\n" \ + "leetleet_neg = -13371337\n" \ + "floaty_thing = 1.1\n" \ + "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n" \ + "coord = (1, 2, 4.5)\n" \ + " # this is just a comment\n" \ + "this is an invalid line\n" \ + "asdf = {\n" \ + " a = 5\n" \ + " bb = 2.5\n" \ + " ccc = \"\"\"\n" \ + "testy\n" \ + " testa \n" \ + "\"\"\"\n" \ + "\n" \ + "}\n" \ + "blarg = \"\"\" \n" \ + "some multiline text\n" \ + " with leading whitespace!\n" \ + "\"\"\"\n" \ + "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.7, 2.4\n" \ + "zoop = true" + +#define TEST_CONFIG_TEXT_AFTER \ + "leet = 1337\n" \ + "leetleet = 13371337\n" \ + "leetleet_neg = -13371337\n" \ + "floaty_thing = 1.1\n" \ + "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n" \ + "coord = (1, 2, 4.5)\n" \ + " # this is just a comment\n" \ + "this is an invalid line\n" \ + "asdf = {\n" \ + " a = 5\n" \ + " bb = 2.5\n" \ + " ccc = \"\"\"\n" \ + "testy\n" \ + " testa \n" \ + "\"\"\"\n" \ + "\n" \ + "}\n" \ + "blarg = \"\"\" \n" \ + "some multiline text\n" \ + " with leading whitespace!\n" \ + "\"\"\"\n" \ + "np_terrain = {\n" \ + " flags = defaults\n" \ + " lacunarity = 2.4\n" \ + " octaves = 6\n" \ + " offset = 3.5\n" \ + " persistence = 0.7\n" \ + " scale = 40\n" \ + " seed = 12341\n" \ + " spread = (250,250,250)\n" \ + "}\n" \ + "zoop = true\n" \ + "coord2 = (1,2,3.3)\n" \ + "floaty_thing_2 = 1.2\n" \ + "groupy_thing = {\n" \ + " animals = cute\n" \ + " num_apples = 4\n" \ + " num_oranges = 53\n" \ + "}\n" + struct TestSettings: public TestBase { void Run() { + try { Settings s; + // Test reading of settings - s.parseConfigLine("leet = 1337"); - s.parseConfigLine("leetleet = 13371337"); - s.parseConfigLine("leetleet_neg = -13371337"); - s.parseConfigLine("floaty_thing = 1.1"); - s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP"); - s.parseConfigLine("coord = (1, 2, 4.5)"); + std::istringstream is(TEST_CONFIG_TEXT_BEFORE); + s.parseConfigLines(is); + UASSERT(s.getS32("leet") == 1337); UASSERT(s.getS16("leetleet") == 32767); UASSERT(s.getS16("leetleet_neg") == -32768); + // Not sure if 1.1 is an exact value as a float, but doesn't matter UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001); UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP"); UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001); UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001); UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001); + // Test the setting of settings too s.setFloat("floaty_thing_2", 1.2); s.setV3F("coord2", v3f(1, 2, 3.3)); @@ -407,6 +514,69 @@ struct TestSettings: public TestBase UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001); UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001); UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001); + + // Test settings groups + Settings *group = s.getGroup("asdf"); + UASSERT(group != NULL); + UASSERT(s.getGroupNoEx("zoop", group) == false); + UASSERT(group->getS16("a") == 5); + UASSERT(fabs(group->getFloat("bb") - 2.5) < 0.001); + + Settings *group3 = new Settings; + group3->set("cat", "meow"); + group3->set("dog", "woof"); + + Settings *group2 = new Settings; + group2->setS16("num_apples", 4); + group2->setS16("num_oranges", 53); + group2->setGroup("animals", group3); + group2->set("animals", "cute"); //destroys group 3 + s.setGroup("groupy_thing", group2); + + // Test set failure conditions + UASSERT(s.set("Zoop = Poop\nsome_other_setting", "false") == false); + UASSERT(s.set("sneaky", "\"\"\"\njabberwocky = false") == false); + UASSERT(s.set("hehe", "asdfasdf\n\"\"\"\nsomething = false") == false); + + // Test multiline settings + UASSERT(group->get("ccc") == "testy\n testa "); + + UASSERT(s.get("blarg") == + "some multiline text\n" + " with leading whitespace!"); + + // Test NoiseParams + UASSERT(s.getEntry("np_terrain").is_group == false); + + NoiseParams np; + UASSERT(s.getNoiseParams("np_terrain", np) == true); + UASSERT(fabs(np.offset - 5) < 0.001); + UASSERT(fabs(np.scale - 40) < 0.001); + UASSERT(fabs(np.spread.X - 250) < 0.001); + UASSERT(fabs(np.spread.Y - 250) < 0.001); + UASSERT(fabs(np.spread.Z - 250) < 0.001); + UASSERT(np.seed == 12341); + UASSERT(np.octaves == 5); + UASSERT(fabs(np.persist - 0.7) < 0.001); + + np.offset = 3.5; + np.octaves = 6; + s.setNoiseParams("np_terrain", np); + + UASSERT(s.getEntry("np_terrain").is_group == true); + + // Test writing + std::ostringstream os(std::ios_base::binary); + is.clear(); + is.seekg(0); + + UASSERT(s.updateConfigObject(is, os, "", 0) == true); + //printf(">>>> expected config:\n%s\n", TEST_CONFIG_TEXT_AFTER); + //printf(">>>> actual config:\n%s\n", os.str().c_str()); + UASSERT(os.str() == TEST_CONFIG_TEXT_AFTER); + } catch (SettingNotFoundException &e) { + UASSERT(!"Setting not found!"); + } } }; @@ -428,7 +598,7 @@ struct TestSerialization: public TestBase UASSERT(serializeWideString(L"") == mkstr("\0\0")); UASSERT(serializeLongString("") == mkstr("\0\0\0\0")); UASSERT(serializeJsonString("") == "\"\""); - + std::string teststring = "Hello world!"; UASSERT(serializeString(teststring) == mkstr("\0\14Hello world!")); @@ -554,12 +724,12 @@ struct TestCompress: public TestBase fromdata[1]=5; fromdata[2]=5; fromdata[3]=1; - + std::ostringstream os(std::ios_base::binary); compress(fromdata, os, 0); std::string str_out = os.str(); - + infostream<<"str_out.size()="< "; for(u32 i=0; i "; for(u32 i=0; iget(n).light_propagates == true); @@ -711,28 +881,28 @@ struct TestVoxelManipulator: public TestBase VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1)); UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1); UASSERT(a.index(-1,-1,-1) == 0); - + VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2)); // An area that is 1 bigger in x+ and z- VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2)); - + std::list aa; d.diff(c, aa); - + // Correct results std::vector results; results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3))); results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2))); UASSERT(aa.size() == results.size()); - + infostream<<"Result of diff:"<::const_iterator i = aa.begin(); i != aa.end(); ++i) { i->print(infostream); infostream<::iterator j = std::find(results.begin(), results.end(), *i); UASSERT(j != results.end()); results.erase(j); @@ -742,13 +912,13 @@ struct TestVoxelManipulator: public TestBase /* VoxelManipulator */ - + VoxelManipulator v; v.print(infostream, nodedef); infostream<<"*** Setting (-1,0,-1)=2 ***"< data1(1); data1[0] = 100; Address a(127,0,0,1, 10); - u16 seqnum = 34352; + const u16 seqnum = 34352; con::BufferedPacket p1 = con::makePacket(a, data1, proto_id, peer_id, channel); @@ -1585,7 +1773,7 @@ struct TestConnection: public TestBase UASSERT(readU16(&p1.data[4]) == peer_id); UASSERT(readU8(&p1.data[6]) == channel); UASSERT(readU8(&p1.data[7]) == data1[0]); - + //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)< p2 = con::makeReliablePacket(data1, seqnum); @@ -1647,28 +1835,29 @@ struct TestConnection: public TestBase Handler hand_server("server"); Handler hand_client("client"); - + infostream<<"** Creating server Connection"<channels[chn]; u16 sn = ch->next_outgoing_seqnum; @@ -1820,7 +2009,7 @@ struct TestConnection: public TestBase UASSERT(size == data1.getSize()); UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0); UASSERT(peer_id == PEER_ID_SERVER); - + infostream<<"** running client.Receive()"< data1(datasize); for(u16 i=0; i recvdata; infostream<<"** running client.Receive()"<