File: AulMathTest.cpp

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (68 lines) | stat: -rw-r--r-- 2,287 bytes parent folder | download | duplicates (5)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2015-2016, The OpenClonk Team and contributors
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
*/

// Testing C4Aul behaviour.

#include "C4Include.h"
#include "AulTest.h"

#include "script/C4Aul.h"

class AulMathTest : public AulTest
{};

TEST_F(AulMathTest, Addition)
{
	// Adding 0 to a value should return the same value.
	EXPECT_EQ(C4VInt(0), RunExpr("0 + 0"));
	EXPECT_EQ(C4VINT_MAX, RunExpr("2147483647 + 0"));
	EXPECT_EQ(C4VINT_MAX, RunExpr("0 + 2147483647"));
	EXPECT_EQ(C4VINT_MIN, RunExpr("-2147483648 + 0"));
	EXPECT_EQ(C4VINT_MIN, RunExpr("0 + -2147483648"));

	EXPECT_EQ(C4VInt(1078530011), RunExpr("1078530011 + 0"));
	EXPECT_EQ(C4VInt(1078530011), RunExpr("0 + 1078530011"));
	EXPECT_EQ(C4VInt(1068827891), RunExpr("1068827891 + 0"));
	EXPECT_EQ(C4VInt(1068827891), RunExpr("0 + 1068827891"));
}

TEST_F(AulMathTest, Division)
{
	// Dividing a value by 0 should fail.
	EXPECT_THROW(RunExpr("1 / 0"), C4AulExecError);
	// Dividing C4VINT_MIN by -1 should fail.
	EXPECT_THROW(RunExpr("-2147483648 / -1"), C4AulExecError);
	// Dividing C4VINT_MIN by 1 should return C4VINT_MIN.
	EXPECT_EQ(C4VINT_MIN, RunExpr("-2147483648 / 1"));
}

TEST_F(AulMathTest, Bug1389)
{
	// 0001389: Aul integer overflow results in strange numbers on 64 bit
	// machines

	// "Strange numbers" shall mean numbers that are impossible to express in
	// C4Script as an integer literal. In particular, in these cases, the
	// high dword of the numbers, which is not exposed to C4Script, is not
	// guaranteed to be zero. Therefore, straight comparison of these numbers
	// will result in unexpected results.

	EXPECT_EQ(C4VINT_MIN, RunExpr("2147483647 + 1"));
	EXPECT_EQ(C4VINT_MAX, RunExpr("-2147483648 - 1"));
	// x ± 1 ± 1 is handled differently from x ± 2, yet the result should be
	// the same.
	EXPECT_EQ(C4Value(true), RunExpr("2147483647 + 1 + 1 == 2147483647 + 2"));
	EXPECT_EQ(C4Value(true), RunExpr("-2147483648 - 1 - 1 == -2147483648 - 2"));
}