File: large.cpp

package info (click to toggle)
sol2 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,096 kB
  • sloc: cpp: 43,816; ansic: 1,018; python: 356; sh: 288; makefile: 202
file content (168 lines) | stat: -rw-r--r-- 5,844 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// sol2

// The MIT License (MIT)

// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <catch2/catch_all.hpp>

#include <sol/sol.hpp>

#include <cstdint>
#include <limits>

inline namespace sol2_tests_large_integer {
	inline bool bool_roundtrip(bool num) {
		REQUIRE(num == true);
		return num;
	}

	inline void trigger_passthrough_crash(sol::state& lua) {
		sol::protected_function pf = lua["f"];
		auto result = pf(0xFFFFFFFFFFFFFFFFull);
		(void)result;
	}

	template <typename T>
	inline T intT_passthrough(T num) {
		return num;
	}

	template <typename T>
	inline T intT_roundtrip(T num) {
		REQUIRE(num == std::numeric_limits<T>::max());
		return num;
	}

	inline std::uint64_t int53_roundtrip(std::uint64_t num) {
		REQUIRE(num == 0x1FFFFFFFFFFFFFull);
		return num;
	}
} // namespace sol2_tests_large_integer

TEST_CASE("large_integer/bool", "pass bool integral value to and from lua") {
	sol::state lua;
	lua.open_libraries();
	lua.set_function("f", bool_roundtrip);
	sol::optional<sol::error> result1 = lua.safe_script(
	     "x = f(true)\n"
	     "assert(x == true)",
	     sol::script_pass_on_error);
	REQUIRE_FALSE(result1.has_value());
	sol::object x = lua["x"];
	REQUIRE(x.is<bool>());
	REQUIRE(x.as<bool>() == true);
	REQUIRE_FALSE(x.is<std::int32_t>());
	{
		auto result = lua.safe_script("f(1)", sol::script_pass_on_error);
		REQUIRE_FALSE(result.valid());
	}
}

TEST_CASE("large_integers/unsigned32", "pass large unsigned 32bit values to and from lua") {
	using T = std::uint32_t;
	sol::state lua;
	lua.open_libraries();
	lua.set_function("f", intT_roundtrip<T>);
	auto result1 = lua.safe_script(
	     "x = f(0xFFFFFFFF)\n"
	     "assert(x == 0xFFFFFFFF)",
	     sol::script_pass_on_error);
	REQUIRE(result1.valid());
	sol::object x = lua["x"];
	REQUIRE(x.is<T>());
	REQUIRE(x.as<T>() == 0xFFFFFFFF);
}

TEST_CASE("large_integer/unsigned53", "pass large unsigned 53bit value to and from lua") {
	using T = std::uint64_t;
	sol::state lua;
	lua.open_libraries();
	lua.set_function("f", int53_roundtrip);
	auto result1 = lua.safe_script(
	     "x = f(0x1FFFFFFFFFFFFF)\n"
	     "assert(x == 0x1FFFFFFFFFFFFF)");
	REQUIRE(result1.valid());
	sol::object x = lua["x"];
	REQUIRE(x.is<T>());
	REQUIRE(x.as<T>() == 0x1FFFFFFFFFFFFFull);
}

TEST_CASE("large_integer/unsigned64", "pass too large unsigned 64bit value to lua") {
	using T = std::int64_t;
	sol::state lua;
	lua.set_function("f", intT_passthrough<T>);
	REQUIRE_THROWS(trigger_passthrough_crash(lua));
}

TEST_CASE("large_integer/double", "pass negative and large positive values as signed and unsigned from and to lua") {
	sol::state lua;
	lua.open_libraries();
	lua.set_function("s32", intT_passthrough<std::int32_t>);
	lua.set_function("s64", intT_passthrough<std::int64_t>);
	lua.set_function("u32", intT_passthrough<std::uint32_t>);
	lua.set_function("u64", intT_passthrough<std::uint64_t>);
	{
		// signed 32bit
		auto result1 = lua.safe_script("x = s32(-1)", sol::script_pass_on_error);
		REQUIRE(result1.valid());
		auto result2 = lua.safe_script("assert(x == -1)", sol::script_pass_on_error);
		REQUIRE(result2.valid());
		auto result3 = lua.safe_script("x = s32(0xFFFFFFFF)", sol::script_pass_on_error);
		REQUIRE(result3.valid());
		auto result4 = lua.safe_script("assert(x == -1)", sol::script_pass_on_error);
		REQUIRE(result4.valid());

		sol::object x = lua["x"];
		REQUIRE(x.is<std::int32_t>());
		REQUIRE(x.as<std::int32_t>() == -1);
		REQUIRE(x.is<std::uint32_t>());
		REQUIRE(x.as<std::uint32_t>() == 0xFFFFFFFF);
	}

	// unsigned 32bit
	{
		auto result1 = lua.safe_script("x = u32(0xFFFFFFFF)", sol::script_pass_on_error);
		REQUIRE(result1.valid());
		auto result2 = lua.safe_script("assert(x == 0xFFFFFFFF)", sol::script_pass_on_error);
		REQUIRE(result2.valid());
		auto result3 = lua.safe_script("x = u32(-1)", sol::script_pass_on_error);
		REQUIRE(result3.valid());
		auto result4 = lua.safe_script("assert(x == 0xFFFFFFFF)", sol::script_pass_on_error);
		REQUIRE(result4.valid());
		sol::object x = lua["x"];
		REQUIRE(x.is<std::int32_t>());
		REQUIRE(x.as<std::int32_t>() == -1);
		REQUIRE(x.is<std::uint32_t>());
		REQUIRE(x.as<std::uint32_t>() == 0xFFFFFFFF);
	}
	// signed 64bit
	{
		auto result1 = lua.safe_script("x = s64(-1)", sol::script_pass_on_error);
		REQUIRE(result1.valid());
		auto result2 = lua.safe_script("assert(x == -1)", sol::script_pass_on_error);
		REQUIRE(result2.valid());
		sol::object x = lua["x"];
		REQUIRE(x.is<std::int64_t>());
		REQUIRE(x.as<std::int64_t>() == -1);
		REQUIRE(x.is<std::uint64_t>());
		REQUIRE(x.as<std::uint64_t>() == 0xFFFFFFFFFFFFFFFFull);
	}
}