Backed out 5 changesets (bug 1890092, bug 1888683) for causing build bustages & crash...
[gecko.git] / third_party / rust / uniffi-example-rondpoint / tests / bindings / test_rondpoint.rb
blob0121f6e0f91a935341ff36da9846119d66c6503c
1 # frozen_string_literal: true
3 require 'test/unit'
4 require 'rondpoint'
6 include Test::Unit::Assertions
7 include Rondpoint
9 dico = Dictionnaire.new Enumeration::DEUX, true, 0, 123_456_789
11 assert_equal dico, Rondpoint.copie_dictionnaire(dico)
13 assert_equal Rondpoint.copie_enumeration(Enumeration::DEUX), Enumeration::DEUX
15 assert_equal Rondpoint.copie_enumerations([
16                                             Enumeration::UN,
17                                             Enumeration::DEUX
18                                           ]), [Enumeration::UN, Enumeration::DEUX]
20 assert_equal Rondpoint.copie_carte({
21                                      '0' => EnumerationAvecDonnees::ZERO.new,
22                                      '1' => EnumerationAvecDonnees::UN.new(1),
23                                      '2' => EnumerationAvecDonnees::DEUX.new(2, 'deux')
24                                    }), {
25                                      '0' => EnumerationAvecDonnees::ZERO.new,
26                                      '1' => EnumerationAvecDonnees::UN.new(1),
27                                      '2' => EnumerationAvecDonnees::DEUX.new(2, 'deux')
28                                    }
30 assert Rondpoint.switcheroo(false)
32 assert_not_equal EnumerationAvecDonnees::ZERO.new, EnumerationAvecDonnees::UN.new(1)
33 assert_equal EnumerationAvecDonnees::UN.new(1), EnumerationAvecDonnees::UN.new(1)
34 assert_not_equal EnumerationAvecDonnees::UN.new(1), EnumerationAvecDonnees::UN.new(2)
36 # Test the roundtrip across the FFI.
37 # This shows that the values we send come back in exactly the same state as we sent them.
38 # i.e. it shows that lowering from ruby and lifting into rust is symmetrical with
39 #      lowering from rust and lifting into ruby.
40 RT = Retourneur.new
42 def affirm_aller_retour(vals, fn_name)
43   vals.each do |v|
44     id_v = RT.public_send fn_name, v
46     assert_equal id_v, v, "Round-trip failure: #{v} => #{id_v}"
47   end
48 end
50 MIN_I8 = -1 * 2**7
51 MAX_I8 = 2**7 - 1
52 MIN_I16 = -1 * 2**15
53 MAX_I16 = 2**15 - 1
54 MIN_I32 = -1 * 2**31
55 MAX_I32 = 2**31 - 1
56 MIN_I64 = -1 * 2**31
57 MAX_I64 = 2**31 - 1
59 # Ruby floats are always doubles, so won't round-trip through f32 correctly.
60 # This truncates them appropriately.
61 F32_ONE_THIRD = [1.0 / 3].pack('f').unpack('f')[0]
63 # Booleans
64 affirm_aller_retour([true, false], :identique_boolean)
66 # Bytes.
67 affirm_aller_retour([MIN_I8, -1, 0, 1, MAX_I8], :identique_i8)
68 affirm_aller_retour([0x00, 0x12, 0xFF], :identique_u8)
70 # Shorts
71 affirm_aller_retour([MIN_I16, -1, 0, 1, MAX_I16], :identique_i16)
72 affirm_aller_retour([0x0000, 0x1234, 0xFFFF], :identique_u16)
74 # Ints
75 affirm_aller_retour([MIN_I32, -1, 0, 1, MAX_I32], :identique_i32)
76 affirm_aller_retour([0x00000000, 0x12345678, 0xFFFFFFFF], :identique_u32)
78 # Longs
79 affirm_aller_retour([MIN_I64, -1, 0, 1, MAX_I64], :identique_i64)
80 affirm_aller_retour([0x0000000000000000, 0x1234567890ABCDEF, 0xFFFFFFFFFFFFFFFF], :identique_u64)
82 # Floats
83 affirm_aller_retour([0.0, 0.5, 0.25, 1.0, F32_ONE_THIRD], :identique_float)
85 # Doubles
86 affirm_aller_retour(
87   [0.0, 0.5, 0.25, 1.0, 1.0 / 3, Float::MAX, Float::MIN],
88   :identique_double
91 # Strings
92 affirm_aller_retour(
93   ['', 'abc', 'été', 'ښي لاس ته لوستلو لوستل',
94    '😻emoji 👨‍👧‍👦multi-emoji, 🇨🇭a flag, a canal, panama'],
95   :identique_string
98 # Test one way across the FFI.
100 # We send one representation of a value to lib.rs, and it transforms it into another, a string.
101 # lib.rs sends the string back, and then we compare here in ruby.
103 # This shows that the values are transformed into strings the same way in both ruby and rust.
104 # i.e. if we assume that the string return works (we test this assumption elsewhere)
105 #      we show that lowering from ruby and lifting into rust has values that both ruby and rust
106 #      both stringify in the same way. i.e. the same values.
108 # If we roundtripping proves the symmetry of our lowering/lifting from here to rust, and lowering/lifting from rust to here,
109 # and this convinces us that lowering/lifting from here to rust is correct, then
110 # together, we've shown the correctness of the return leg.
111 ST = Stringifier.new
113 def affirm_enchaine(vals, fn_name)
114   vals.each do |v|
115     str_v = ST.public_send fn_name, v
117     assert_equal v.to_s, str_v, "String compare error #{v} => #{str_v}"
118   end
121 # Test the efficacy of the string transport from rust. If this fails, but everything else
122 # works, then things are very weird.
123 assert_equal ST.well_known_string('ruby'), 'uniffi 💚 ruby!'
125 # Booleans
126 affirm_enchaine([true, false], :to_string_boolean)
128 # Bytes.
129 affirm_enchaine([MIN_I8, -1, 0, 1, MAX_I8], :to_string_i8)
130 affirm_enchaine([0x00, 0x12, 0xFF], :to_string_u8)
132 # Shorts
133 affirm_enchaine([MIN_I16, -1, 0, 1, MAX_I16], :to_string_i16)
134 affirm_enchaine([0x0000, 0x1234, 0xFFFF], :to_string_u16)
136 # Ints
137 affirm_enchaine([MIN_I32, -1, 0, 1, MAX_I32], :to_string_i32)
138 affirm_enchaine([0x00000000, 0x12345678, 0xFFFFFFFF], :to_string_u32)
140 # Longs
141 affirm_enchaine([MIN_I64, -1, 0, 1, MAX_I64], :to_string_i64)
142 affirm_enchaine([0x0000000000000000, 0x1234567890ABCDEF, 0xFFFFFFFFFFFFFFFF], :to_string_u64)