templates.test_compare

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    class C(sp.Contract):
 7        @sp.entrypoint
 8        def compare_inferior(self):
 9            assert sp.compare(sp.nat(1), sp.nat(5)) == -1
10            assert sp.compare(sp.int(1), sp.int(5)) == -1
11            assert sp.compare(sp.timestamp(1), sp.timestamp(5)) == -1
12            assert (
13                sp.compare(
14                    sp.address("KT18amZmM5W7qDWVt2pH6uj7sCEd3kbzLrHT"),
15                    sp.address("KT1XvNYseNDJJ6Kw27qhSEDF8ys8JhDopzfG"),
16                )
17                == -1
18            )
19            assert sp.compare(False, True) == -1
20            assert sp.compare(sp.bytes("0x00"), sp.bytes("0x01")) == -1
21            assert sp.compare(sp.chain_id("0x00"), sp.chain_id("0x01")) == -1
22            assert (
23                sp.compare(
24                    sp.key("edpkuBknW28nW72KG6RoH"), sp.key("edpkuJqtDcA2m2muMxViS")
25                )
26                == -1
27            )
28            assert (
29                sp.compare(
30                    sp.key_hash("tz1KqTpEZ7Yob7QbPE4Hy"),
31                    sp.key_hash("tz1XPTDmvT3vVE5Uunngm"),
32                )
33                == -1
34            )
35            assert sp.compare(sp.mutez(1), sp.mutez(5)) == -1
36            assert sp.compare("a", "e") == -1
37            assert sp.compare((1, 5), (5, 1)) == -1
38            assert sp.compare(None, sp.Some(())) == -1
39            assert sp.compare(sp.Some(1), sp.Some(5)) == -1
40
41            assert (
42                sp.compare(sp.cast(sp.Left(5), sp.or_[sp.nat, sp.nat]), sp.Right(1))
43                == -1
44            )
45            assert (
46                sp.compare(sp.cast(sp.Left(1), sp.or_[sp.nat, sp.nat]), sp.Left(5))
47                == -1
48            )
49            assert (
50                sp.compare(sp.cast(sp.Left(1), sp.or_[sp.nat, sp.nat]), sp.Left(5))
51                == -1
52            )
53            assert (
54                sp.compare(sp.cast(sp.Right(1), sp.or_[sp.nat, sp.nat]), sp.Right(5))
55                == -1
56            )
57
58        @sp.entrypoint
59        def compare_eq(self):
60            assert sp.compare((), ()) == 0
61            assert (
62                sp.compare(
63                    sp.cast(None, sp.option[sp.nat]), sp.cast(None, sp.option[sp.nat])
64                )
65                == 0
66            )
67            assert sp.compare(sp.nat(2), sp.nat(1)) == 1
68
69
70@sp.add_test()
71def test():
72    scenario = sp.test_scenario("Compare", main)
73    scenario.h1("Compare")
74    c = main.C()
75    scenario += c
76    c.compare_inferior()
77    c.compare_eq()