gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] branch master updated: re-add test cases


From: gnunet
Subject: [taler-wallet-core] branch master updated: re-add test cases
Date: Thu, 16 Jul 2020 14:04:38 +0200

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository wallet-core.

The following commit(s) were added to refs/heads/master by this push:
     new d17da64a re-add test cases
d17da64a is described below

commit d17da64a3541258f6662bfe3016bd87fddf40a41
Author: Florian Dold <florian.dold@gmail.com>
AuthorDate: Thu Jul 16 17:34:27 2020 +0530

    re-add test cases
---
 src/util/amounts-test.ts | 142 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)

diff --git a/src/util/amounts-test.ts b/src/util/amounts-test.ts
new file mode 100644
index 00000000..e10ee596
--- /dev/null
+++ b/src/util/amounts-test.ts
@@ -0,0 +1,142 @@
+/*
+ This file is part of GNU Taler
+ (C) 2020 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+import test from "ava";
+
+import { Amounts, AmountJson } from "../util/amounts";
+
+const jAmt = (
+  value: number,
+  fraction: number,
+  currency: string,
+): AmountJson => ({ value, fraction, currency });
+
+const sAmt = (
+  s: string
+): AmountJson => Amounts.parseOrThrow(s);
+
+test("amount addition (simple)", (t) => {
+  const a1 = jAmt(1, 0, "EUR");
+  const a2 = jAmt(1, 0, "EUR");
+  const a3 = jAmt(2, 0, "EUR");
+  t.true(0 === Amounts.cmp(Amounts.add(a1, a2).amount, a3));
+  t.pass();
+});
+
+test("amount addition (saturation)", (t) => {
+  const a1 = jAmt(1, 0, "EUR");
+  const res = Amounts.add(jAmt(Amounts.maxAmountValue, 0, "EUR"), a1);
+  t.true(res.saturated);
+  t.pass();
+});
+
+test("amount subtraction (simple)", (t) => {
+  const a1 = jAmt(2, 5, "EUR");
+  const a2 = jAmt(1, 0, "EUR");
+  const a3 = jAmt(1, 5, "EUR");
+  t.true(0 === Amounts.cmp(Amounts.sub(a1, a2).amount, a3));
+  t.pass();
+});
+
+test("amount subtraction (saturation)", (t) => {
+  const a1 = jAmt(0, 0, "EUR");
+  const a2 = jAmt(1, 0, "EUR");
+  let res = Amounts.sub(a1, a2);
+  t.true(res.saturated);
+  res = Amounts.sub(a1, a1);
+  t.true(!res.saturated);
+  t.pass();
+});
+
+test("amount comparison", (t) => {
+  t.is(Amounts.cmp(jAmt(1, 0, "EUR"), jAmt(1, 0, "EUR")), 0);
+  t.is(Amounts.cmp(jAmt(1, 1, "EUR"), jAmt(1, 0, "EUR")), 1);
+  t.is(Amounts.cmp(jAmt(1, 1, "EUR"), jAmt(1, 2, "EUR")), -1);
+  t.is(Amounts.cmp(jAmt(1, 0, "EUR"), jAmt(0, 0, "EUR")), 1);
+  t.is(Amounts.cmp(jAmt(0, 0, "EUR"), jAmt(1, 0, "EUR")), -1);
+  t.is(Amounts.cmp(jAmt(1, 0, "EUR"), jAmt(0, 100000000, "EUR")), 0);
+  t.throws(() => Amounts.cmp(jAmt(1, 0, "FOO"), jAmt(1, 0, "BAR")));
+  t.pass();
+});
+
+test("amount parsing", (t) => {
+  t.is(
+    Amounts.cmp(Amounts.parseOrThrow("TESTKUDOS:0"), jAmt(0, 0, "TESTKUDOS")),
+    0,
+  );
+  t.is(
+    Amounts.cmp(Amounts.parseOrThrow("TESTKUDOS:10"), jAmt(10, 0, 
"TESTKUDOS")),
+    0,
+  );
+  t.is(
+    Amounts.cmp(
+      Amounts.parseOrThrow("TESTKUDOS:0.1"),
+      jAmt(0, 10000000, "TESTKUDOS"),
+    ),
+    0,
+  );
+  t.is(
+    Amounts.cmp(
+      Amounts.parseOrThrow("TESTKUDOS:0.00000001"),
+      jAmt(0, 1, "TESTKUDOS"),
+    ),
+    0,
+  );
+  t.is(
+    Amounts.cmp(
+      Amounts.parseOrThrow("TESTKUDOS:4503599627370496.99999999"),
+      jAmt(4503599627370496, 99999999, "TESTKUDOS"),
+    ),
+    0,
+  );
+  t.throws(() => Amounts.parseOrThrow("foo:"));
+  t.throws(() => Amounts.parseOrThrow("1.0"));
+  t.throws(() => Amounts.parseOrThrow("42"));
+  t.throws(() => Amounts.parseOrThrow(":1.0"));
+  t.throws(() => Amounts.parseOrThrow(":42"));
+  t.throws(() => Amounts.parseOrThrow("EUR:.42"));
+  t.throws(() => Amounts.parseOrThrow("EUR:42."));
+  t.throws(() => Amounts.parseOrThrow("TESTKUDOS:4503599627370497.99999999"));
+  t.is(
+    Amounts.cmp(
+      Amounts.parseOrThrow("TESTKUDOS:0.99999999"),
+      jAmt(0, 99999999, "TESTKUDOS"),
+    ),
+    0,
+  );
+  t.throws(() => Amounts.parseOrThrow("TESTKUDOS:0.999999991"));
+  t.pass();
+});
+
+test("amount stringification", (t) => {
+  t.is(Amounts.stringify(jAmt(0, 0, "TESTKUDOS")), "TESTKUDOS:0");
+  t.is(Amounts.stringify(jAmt(4, 94000000, "TESTKUDOS")), "TESTKUDOS:4.94");
+  t.is(Amounts.stringify(jAmt(0, 10000000, "TESTKUDOS")), "TESTKUDOS:0.1");
+  t.is(Amounts.stringify(jAmt(0, 1, "TESTKUDOS")), "TESTKUDOS:0.00000001");
+  t.is(Amounts.stringify(jAmt(5, 0, "TESTKUDOS")), "TESTKUDOS:5");
+  // denormalized
+  t.is(Amounts.stringify(jAmt(1, 100000000, "TESTKUDOS")), "TESTKUDOS:2");
+  t.pass();
+});
+
+test("amount multiplication", (t) => {
+  t.is(Amounts.stringify(Amounts.mult(sAmt("EUR:1.11"), 0).amount), "EUR:0");
+  t.is(Amounts.stringify(Amounts.mult(sAmt("EUR:1.11"), 1).amount), 
"EUR:1.11");
+  t.is(Amounts.stringify(Amounts.mult(sAmt("EUR:1.11"), 2).amount), 
"EUR:2.22");
+  t.is(Amounts.stringify(Amounts.mult(sAmt("EUR:1.11"), 3).amount), 
"EUR:3.33");
+  t.is(Amounts.stringify(Amounts.mult(sAmt("EUR:1.11"), 4).amount), 
"EUR:4.44");
+  t.is(Amounts.stringify(Amounts.mult(sAmt("EUR:1.11"), 5).amount), 
"EUR:5.55");
+});

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]