aboutsummaryrefslogtreecommitdiff
path: root/builtin/common/tests/misc_helpers_spec.lua
blob: bb9d13e7f00389d08c6ba70e5c57eee396b745bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distr_G.core = {}
dofile("builtin/common/misc_helpers.lua")

describe("string", function()
	it("trim()", function()
		assert.equal("foo bar", string/****************
 * Black magic! *
 ****************
 * The position hashing is very messed up.
 * It's a lot more complicated than it looks.
 */

static inline s16 unsigned_to_signed(u16 i, u16 max_positive)
{
	if (i < max_positive) {
		return i;
	} else {
		return i - (max_positive * 2);
	}
}


// Modulo of a negative number does not work consistently in C
static inline s64 pythonmodulo(s64 i, s16 mod)
{
	if (i >= 0) {
		return i % mod;
	}
	return mod - ((-i) % mod);
}


s64 Database::getBlockAsInteger(const v3s16 &pos)
{
	return (u64) pos.Z * 0x1000000 +
		(u64) pos.Y * 0x1000 +
		(u64) pos.X;
}


v3s16 Database::getIntegerAsBlock(s64 i)
{
	v3s16 pos;
	pos.X = unsigned_to_signed(pythonmodulo(i, 4096), 2048);
	i = (i - pos.X) / 4096;
	pos.Y = unsigned_to_signed(pythonmodulo(i, 4096), 2048);
	i = (i - pos.Y) / 4096;
	pos.Z = unsigned_to_signed(pythonmodulo(i, 4096), 2048);
	return pos;
}

ss="hl opt">.split("hello,world", ",", true)) assert.same({ "hello", "world", "" }, string.split("hello,world,", ",", true)) assert.same({ "hello", "", "", "world", "2" }, string.split("hello,,,world,2", ",", true)) assert.same({ "", "", "hello", "world", "2" }, string.split(",,hello,world,2", ",", true)) assert.same({ "hello ", " world | :" }, string.split("hello :| world | :", ":|")) end) it("max_splits", function() assert.same({ "one" }, string.split("one", ",", true, 2)) assert.same({ "one,two,three,four" }, string.split("one,two,three,four", ",", true, 0)) assert.same({ "one", "two", "three,four" }, string.split("one,two,three,four", ",", true, 2)) assert.same({ "one", "", "two,three,four" }, string.split("one,,two,three,four", ",", true, 2)) assert.same({ "one", "two", "three,four" }, string.split("one,,,,,,two,three,four", ",", false, 2)) end) it("pattern", function() assert.same({ "one", "two" }, string.split("one,two", ",", false, -1, true)) assert.same({ "one", "two", "three" }, string.split("one2two3three", "%d", false, -1, true)) end) end) end) describe("privs", function() it("from string", function() assert.same({ a = true, b = true }, core.string_to_privs("a,b")) end) it("to string", function() assert.equal("one", core.privs_to_string({ one=true })) local ret = core.privs_to_string({ a=true, b=true }) assert(ret == "a,b" or ret == "b,a") end) end) describe("pos", function() it("from string", function() assert.same({ x = 10, y = 5.1, z = -2}, core.string_to_pos("10.0, 5.1, -2")) assert.same({ x = 10, y = 5.1, z = -2}, core.string_to_pos("( 10.0, 5.1, -2)")) assert.is_nil(core.string_to_pos("asd, 5, -2)")) end) it("to string", function() assert.equal("(10.1,5.2,-2.3)", core.pos_to_string({ x = 10.1, y = 5.2, z = -2.3})) end) end) describe("table", function() it("indexof()", function() assert.equal(1, table.indexof({"foo", "bar"}, "foo")) assert.equal(-1, table.indexof({"foo", "bar"}, "baz")) end) end)