Tarantool development patches archive
 help / color / mirror / Atom feed
From: Serge Petrenko <sergepetrenko@tarantool.org>
To: vdavydov.dev@gmail.com, kostja@tarantool.org
Cc: tarantool-patches@freelists.org,
	Serge Petrenko <sergepetrenko@tarantool.org>
Subject: [PATCH v3 1/6] lua: fix decimal comparison with nil
Date: Tue, 20 Aug 2019 20:09:59 +0300	[thread overview]
Message-ID: <6f199383d4b5bbe726d55ad4d40db9dfd67a5a02.1566320473.git.sergepetrenko@tarantool.org> (raw)
In-Reply-To: <cover.1566320473.git.sergepetrenko@tarantool.org>

Previously decimal comparison with nil failed with following error:
`expected decimal, number or string as 2 argument`.
Fix this. Throw a more verbose error in case of '>', '<', '>=', '<='
and fix equality check.

Follow-up #692
---
 src/lua/decimal.c         | 19 ++++++++++++++++++-
 test/app/decimal.result   | 26 ++++++++++++++++++++++++++
 test/app/decimal.test.lua |  8 ++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/src/lua/decimal.c b/src/lua/decimal.c
index de6586c8b..7f9358787 100644
--- a/src/lua/decimal.c
+++ b/src/lua/decimal.c
@@ -69,6 +69,10 @@ ldecimal_##name(struct lua_State *L) {						\
 static int									\
 ldecimal_##name(struct lua_State *L) {						\
 	assert(lua_gettop(L) == 2);						\
+	if (lua_isnil(L, 1) || lua_isnil(L, 2)) {				\
+		luaL_error(L, "attempt to compare decimal with nil");		\
+		return 1;							\
+	}									\
 	decimal_t *lhs = lua_todecimal(L, 1);					\
 	decimal_t *rhs = lua_todecimal(L, 2);					\
 	lua_pushboolean(L, decimal_compare(lhs, rhs) cmp 0);			\
@@ -226,10 +230,23 @@ LDECIMAL_FUNC(exp, exp)
 LDECIMAL_FUNC(sqrt, sqrt)
 LDECIMAL_FUNC(abs, abs)
 
-LDECIMAL_CMPOP(eq, ==)
 LDECIMAL_CMPOP(lt, <)
 LDECIMAL_CMPOP(le, <=)
 
+static int
+ldecimal_eq(struct lua_State *L)
+{
+	assert(lua_gettop(L) == 2);
+	if (lua_isnil(L, 1) || lua_isnil(L, 2)) {
+		lua_pushboolean(L, false);
+		return 1;
+	}
+	decimal_t *lhs = lua_todecimal(L, 1);
+	decimal_t *rhs = lua_todecimal(L, 2);
+	lua_pushboolean(L, decimal_compare(lhs, rhs) == 0);
+	return 1;
+}
+
 static int
 ldecimal_minus(struct lua_State *L)
 {
diff --git a/test/app/decimal.result b/test/app/decimal.result
index c632f57a7..2e44928bb 100644
--- a/test/app/decimal.result
+++ b/test/app/decimal.result
@@ -223,6 +223,32 @@ b
  | - '0.1'
  | ...
 
+-- check comparsion with nil
+a == nil
+ | ---
+ | - false
+ | ...
+a ~= nil
+ | ---
+ | - true
+ | ...
+a > nil
+ | ---
+ | - error: '[string "return a > nil "]:1: attempt to compare decimal with nil'
+ | ...
+a < nil
+ | ---
+ | - error: '[string "return a < nil "]:1: attempt to compare decimal with nil'
+ | ...
+a >= nil
+ | ---
+ | - error: '[string "return a >= nil "]:1: attempt to compare decimal with nil'
+ | ...
+a <= nil
+ | ---
+ | - error: '[string "return a <= nil "]:1: attempt to compare decimal with nil'
+ | ...
+
 decimal.sqrt(a)
  | ---
  | - '3.1622776601683793319988935444327185337'
diff --git a/test/app/decimal.test.lua b/test/app/decimal.test.lua
index 40f1f29de..d83522b45 100644
--- a/test/app/decimal.test.lua
+++ b/test/app/decimal.test.lua
@@ -62,6 +62,14 @@ a ~= b
 a
 b
 
+-- check comparsion with nil
+a == nil
+a ~= nil
+a > nil
+a < nil
+a >= nil
+a <= nil
+
 decimal.sqrt(a)
 decimal.ln(a)
 decimal.log10(a)
-- 
2.20.1 (Apple Git-117)

  reply	other threads:[~2019-08-20 17:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-20 17:09 [PATCH v3 0/6] Decimal indices Serge Petrenko
2019-08-20 17:09 ` Serge Petrenko [this message]
2019-08-21 14:13   ` [PATCH v3 1/6] lua: fix decimal comparison with nil Vladimir Davydov
2019-08-20 17:10 ` [PATCH v3 2/6] decimal: fix encoding numbers with positive exponent Serge Petrenko
2019-08-21 14:13   ` Vladimir Davydov
2019-08-20 17:10 ` [PATCH v3 3/6] lua/pickle: fix a typo Serge Petrenko
2019-08-21 14:13   ` Vladimir Davydov
2019-08-20 17:10 ` [PATCH v3 4/6] decimal: allow to encode/decode decimals as MsgPack Serge Petrenko
2019-08-21 15:02   ` Vladimir Davydov
2019-08-20 17:10 ` [PATCH v3 5/6] decimal: add conversions to (u)int64_t Serge Petrenko
2019-08-21 15:02   ` Vladimir Davydov
2019-08-20 17:10 ` [PATCH v3 6/6] decimal: allow to index decimals Serge Petrenko
2019-08-21 15:02   ` Vladimir Davydov
2019-08-22 10:33   ` [tarantool-patches] " Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6f199383d4b5bbe726d55ad4d40db9dfd67a5a02.1566320473.git.sergepetrenko@tarantool.org \
    --to=sergepetrenko@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v3 1/6] lua: fix decimal comparison with nil' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox