1 # frozen_string_literal: true
6 include Test::Unit::Assertions
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([
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')
25 '0' => EnumerationAvecDonnees::ZERO.new,
26 '1' => EnumerationAvecDonnees::UN.new(1),
27 '2' => EnumerationAvecDonnees::DEUX.new(2, 'deux')
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.
42 def affirm_aller_retour(vals, fn_name)
44 id_v = RT.public_send fn_name, v
46 assert_equal id_v, v, "Round-trip failure: #{v} => #{id_v}"
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]
64 affirm_aller_retour([true, false], :identique_boolean)
67 affirm_aller_retour([MIN_I8, -1, 0, 1, MAX_I8], :identique_i8)
68 affirm_aller_retour([0x00, 0x12, 0xFF], :identique_u8)
71 affirm_aller_retour([MIN_I16, -1, 0, 1, MAX_I16], :identique_i16)
72 affirm_aller_retour([0x0000, 0x1234, 0xFFFF], :identique_u16)
75 affirm_aller_retour([MIN_I32, -1, 0, 1, MAX_I32], :identique_i32)
76 affirm_aller_retour([0x00000000, 0x12345678, 0xFFFFFFFF], :identique_u32)
79 affirm_aller_retour([MIN_I64, -1, 0, 1, MAX_I64], :identique_i64)
80 affirm_aller_retour([0x0000000000000000, 0x1234567890ABCDEF, 0xFFFFFFFFFFFFFFFF], :identique_u64)
83 affirm_aller_retour([0.0, 0.5, 0.25, 1.0, F32_ONE_THIRD], :identique_float)
87 [0.0, 0.5, 0.25, 1.0, 1.0 / 3, Float::MAX, Float::MIN],
93 ['', 'abc', 'été', 'ښي لاس ته لوستلو لوستل',
94 '😻emoji 👨👧👦multi-emoji, 🇨🇭a flag, a canal, panama'],
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.
113 def affirm_enchaine(vals, fn_name)
115 str_v = ST.public_send fn_name, v
117 assert_equal v.to_s, str_v, "String compare error #{v} => #{str_v}"
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!'
126 affirm_enchaine([true, false], :to_string_boolean)
129 affirm_enchaine([MIN_I8, -1, 0, 1, MAX_I8], :to_string_i8)
130 affirm_enchaine([0x00, 0x12, 0xFF], :to_string_u8)
133 affirm_enchaine([MIN_I16, -1, 0, 1, MAX_I16], :to_string_i16)
134 affirm_enchaine([0x0000, 0x1234, 0xFFFF], :to_string_u16)
137 affirm_enchaine([MIN_I32, -1, 0, 1, MAX_I32], :to_string_i32)
138 affirm_enchaine([0x00000000, 0x12345678, 0xFFFFFFFF], :to_string_u32)
141 affirm_enchaine([MIN_I64, -1, 0, 1, MAX_I64], :to_string_i64)
142 affirm_enchaine([0x0000000000000000, 0x1234567890ABCDEF, 0xFFFFFFFFFFFFFFFF], :to_string_u64)