The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_lua.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2016 by the Battle for Wesnoth Project
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #define GETTEXT_DOMAIN "wesnoth-test"
15 
16 #include <boost/test/unit_test.hpp>
18 
19 #include "lua/lualib.h"
20 #include "lua/lauxlib.h"
21 #include "lua/llimits.h"
22 #ifndef _WIN32
23 # include <fenv.h>
24 #endif
25 
26 #pragma GCC diagnostic ignored "-Wold-style-cast"
27 
29 
30 static int lua_round(lua_State * L, double d)
31 {
32  lua_pushnumber(L, d);
33  int res = luaL_checkinteger(L, -1);
34  lua_pop(L, 1);
35  return res;
36 }
37 
38 static int lua_round2(double d)
39 {
40  int res;
41  lua_number2int(res, d);
42  return res;
43 }
44 
45 static int lua_round3(double d)
46 {
48  volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0;
49  return u.l_p[LUA_IEEEENDIANLOC];
50 }
51 
52 BOOST_AUTO_TEST_CASE(fpu_rounding)
53 {
54 #ifndef _WIN32
55  BOOST_CHECK_EQUAL(fegetround(), FE_TONEAREST);
56 #endif
57  double d1 = 0.6;
58  double d2 = 6755399441055744.0;
59  BOOST_CHECK_EQUAL(d1 + d2, 6755399441055745.0);
60 }
61 
62 BOOST_AUTO_TEST_CASE(lua_rounding)
63 {
64  lua_kernel_base kernel(nullptr);
65  lua_State * L = kernel.get_state();
66 
67  BOOST_CHECK_EQUAL(lua_round(L, -2.5), -2);
68  BOOST_CHECK_EQUAL(lua_round(L, -1.5), -2);
69  BOOST_CHECK_EQUAL(lua_round(L, -1 ), -1);
70  BOOST_CHECK_EQUAL(lua_round(L, -0.5), 0);
71  BOOST_CHECK_EQUAL(lua_round(L, 0.4), 0);
72  BOOST_CHECK_EQUAL(lua_round(L, 0.5), 0);
73  BOOST_CHECK_EQUAL(lua_round(L, 0.6), 1);
74  BOOST_CHECK_EQUAL(lua_round(L, 1.5), 2);
75  BOOST_CHECK_EQUAL(lua_round(L, 1.6), 2);
76  BOOST_CHECK_EQUAL(lua_round(L, 2 ), 2);
77  BOOST_CHECK_EQUAL(lua_round(L, 2.5), 2);
78  BOOST_CHECK_EQUAL(lua_round(L, 2.6), 3);
79  BOOST_CHECK_EQUAL(lua_round(L, 3.5), 4);
80 
81 
82  BOOST_CHECK_EQUAL(lua_round2(-1.5), -2);
83  BOOST_CHECK_EQUAL(lua_round2(-1.5), -2);
84  BOOST_CHECK_EQUAL(lua_round3( 0.6), 1);
85  BOOST_CHECK_EQUAL(lua_round3( 0.6), 1);
86 }
87 
88 BOOST_AUTO_TEST_SUITE_END()
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
Definition: lauxlib.cpp:391
BOOST_AUTO_TEST_SUITE(test_map_location)
#define d
BOOST_AUTO_TEST_CASE(fpu_rounding)
Definition: test_lua.cpp:52
#define lua_pop(L, n)
Definition: lua.h:322
#define LUAI_EXTRAIEEE
Definition: llimits.h:218
static int lua_round(lua_State *L, double d)
Definition: test_lua.cpp:30
double l_d
Definition: llimits.h:215
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.cpp:467
static int lua_round3(double d)
Definition: test_lua.cpp:45
GLuint res
Definition: glew.h:9258
#define LUA_IEEEENDIANLOC
Definition: llimits.h:220
#define lua_number2int(i, n)
Definition: llimits.h:235
static int lua_round2(double d)
Definition: test_lua.cpp:38
lua_State * get_state()
LUA_INT32 l_p[2]
Definition: llimits.h:215