aboutsummaryrefslogtreecommitdiff
path: root/tests/ffi/tests.cc
blob: 984e22204e6aad67f91d4d9039cbb2f6e4bbde2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
#include "tests/ffi/tests.h"
#include "tests/ffi/lib.rs.h"
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <memory>
#include <numeric>
#include <stdexcept>
#include <string>
#include <tuple>

extern "C" void cxx_test_suite_set_correct() noexcept;
extern "C" tests::R *cxx_test_suite_get_box() noexcept;
extern "C" bool cxx_test_suite_r_is_correct(const tests::R *) noexcept;

namespace tests {

static constexpr char SLICE_DATA[] = "2020";

C::C(size_t n) : n(n) {}

size_t C::get() const { return this->n; }

size_t C::get2() const { return this->n; }

const size_t &C::getRef() const { return this->n; }

size_t &C::getMut() { return this->n; }

size_t C::set(size_t n) {
  this->n = n;
  return this->n;
}

size_t C::set_succeed(size_t n) { return this->set(n); }

size_t C::get_fail() { throw std::runtime_error("unimplemented"); }

size_t Shared::c_method_on_shared() const noexcept { return 2021; }

const size_t &Shared::c_method_ref_on_shared() const noexcept {
  return this->z;
}

size_t &Shared::c_method_mut_on_shared() noexcept { return this->z; }

void Array::c_set_array(int32_t val) noexcept {
  this->a = {val, val, val, val};
}

const std::vector<uint8_t> &C::get_v() const { return this->v; }

std::vector<uint8_t> &C::get_v() { return this->v; }

size_t c_return_primitive() { return 2020; }

Shared c_return_shared() { return Shared{2020}; }

::A::AShared c_return_ns_shared() { return ::A::AShared{2020}; }

::A::B::ABShared c_return_nested_ns_shared() { return ::A::B::ABShared{2020}; }

rust::Box<R> c_return_box() {
  Shared shared{0};
  rust::Box<Shared> box{shared}; // explicit constructor from const T&
  rust::Box<Shared> other{std::move(shared)}; // explicit constructor from T&&
  box = std::move(other);                     // move assignment
  rust::Box<Shared> box2(*box);               // copy from another Box
  rust::Box<Shared> other2(std::move(other)); // move constructor
  rust::Box<Shared>::in_place(shared.z);      // placement static factory
  rust::Box<Shared>::in_place<size_t>(0);
  return rust::Box<R>::from_raw(cxx_test_suite_get_box());
}

std::unique_ptr<C> c_return_unique_ptr() {
  return std::unique_ptr<C>(new C{2020});
}

std::shared_ptr<C> c_return_shared_ptr() {
  return std::shared_ptr<C>(new C{2020});
}

std::unique_ptr<::H::H> c_return_ns_unique_ptr() {
  return std::unique_ptr<::H::H>(new ::H::H{"hello"});
}

const size_t &c_return_ref(const Shared &shared) { return shared.z; }

const size_t &c_return_ns_ref(const ::A::AShared &shared) { return shared.z; }

const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared) {
  return shared.z;
}

size_t &c_return_mut(Shared &shared) { return shared.z; }

rust::Str c_return_str(const Shared &shared) {
  (void)shared;
  return "2020";
}

rust::Slice<const char> c_return_slice_char(const Shared &shared) {
  (void)shared;
  return rust::Slice<const char>(SLICE_DATA, sizeof(SLICE_DATA));
}

rust::Slice<uint8_t> c_return_mutsliceu8(rust::Slice<uint8_t> slice) {
  return slice;
}

rust::String c_return_rust_string() { return "2020"; }

rust::String c_return_rust_string_lossy() {
  return rust::String::lossy("Hello \xf0\x90\x80World");
}

std::unique_ptr<std::string> c_return_unique_ptr_string() {
  return std::unique_ptr<std::string>(new std::string("2020"));
}

std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8() {
  auto vec = std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
  vec->push_back(86);
  vec->push_back(75);
  vec->push_back(30);
  vec->push_back(9);
  return vec;
}

std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
  auto vec = std::unique_ptr<std::vector<double>>(new std::vector<double>());
  vec->push_back(86.0);
  vec->push_back(75.0);
  vec->push_back(30.0);
  vec->push_back(9.5);
  return vec;
}

std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
  return std::unique_ptr<std::vector<std::string>>(
      new std::vector<std::string>());
}

std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
  auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
  vec->push_back(Shared{1010});
  vec->push_back(Shared{1011});
  return vec;
}

std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque() {
  return std::unique_ptr<std::vector<C>>(new std::vector<C>());
}

const std::vector<uint8_t> &c_return_ref_vector(const C &c) {
  return c.get_v();
}

std::vector<uint8_t> &c_return_mut_vector(C &c) { return c.get_v(); }

rust::Vec<uint8_t> c_return_rust_vec_u8() {
  rust::Vec<uint8_t> vec{2, 0, 2, 0};
  return vec;
}

const rust::Vec<uint8_t> &c_return_ref_rust_vec(const C &c) {
  (void)c;
  throw std::runtime_error("unimplemented");
}

rust::Vec<uint8_t> &c_return_mut_rust_vec(C &c) {
  (void)c;
  throw std::runtime_error("unimplemented");
}

rust::Vec<rust::String> c_return_rust_vec_string() {
  return {"2", "0", "2", "0"};
}

rust::Vec<bool> c_return_rust_vec_bool() { return {true, true, false}; }

size_t c_return_identity(size_t n) { return n; }

size_t c_return_sum(size_t n1, size_t n2) { return n1 + n2; }

Enum c_return_enum(uint16_t n) {
  if (n <= static_cast<uint16_t>(Enum::AVal)) {
    return Enum::AVal;
  } else if (n <= static_cast<uint16_t>(Enum::BVal)) {
    return Enum::BVal;
  } else {
    return Enum::CVal;
  }
}

::A::AEnum c_return_ns_enum(uint16_t n) {
  if (n <= static_cast<uint16_t>(::A::AEnum::AAVal)) {
    return ::A::AEnum::AAVal;
  } else if (n <= static_cast<uint16_t>(::A::AEnum::ABVal)) {
    return ::A::AEnum::ABVal;
  } else {
    return ::A::AEnum::ACVal;
  }
}

::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
  if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABAVal)) {
    return ::A::B::ABEnum::ABAVal;
  } else if (n <= static_cast<uint16_t>(::A::B::ABEnum::ABBVal)) {
    return ::A::B::ABEnum::ABBVal;
  } else {
    return ::A::B::ABEnum::ABCVal;
  }
}

const C *c_return_const_ptr(size_t c) { return new C(c); }

C *c_return_mut_ptr(size_t c) { return new C(c); }

Borrow::Borrow(const std::string &s) : s(s) {}

void Borrow::const_member() const {}

void Borrow::nonconst_member() {}

std::unique_ptr<Borrow> c_return_borrow(const std::string &s) {
  return std::unique_ptr<Borrow>(new Borrow(s));
}

void c_take_primitive(size_t n) {
  if (n == 2020) {
    cxx_test_suite_set_correct();
  }
}

void c_take_shared(Shared shared) {
  if (shared.z == 2020) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ns_shared(::A::AShared shared) {
  if (shared.z == 2020) {
    cxx_test_suite_set_correct();
  }
}

void c_take_nested_ns_shared(::A::B::ABShared shared) {
  if (shared.z == 2020) {
    cxx_test_suite_set_correct();
  }
}

void c_take_box(rust::Box<R> r) {
  if (cxx_test_suite_r_is_correct(&*r)) {
    cxx_test_suite_set_correct();
  }
}

void c_take_unique_ptr(std::unique_ptr<C> c) {
  if (c->get() == 2020) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ref_r(const R &r) {
  if (cxx_test_suite_r_is_correct(&r)) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ref_c(const C &c) {
  if (c.get() == 2020) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ref_ns_c(const ::H::H &h) {
  if (h.h == "hello") {
    cxx_test_suite_set_correct();
  }
}

void c_take_str(rust::Str s) {
  if (std::string(s) == "2020") {
    cxx_test_suite_set_correct();
  }
}

void c_take_slice_char(rust::Slice<const char> s) {
  if (std::string(s.data(), s.size()) == "2020") {
    cxx_test_suite_set_correct();
  }
}

void c_take_slice_shared(rust::Slice<const Shared> s) {
  if (s.size() == 2 && s.data()->z == 2020 && s[1].z == 2021 &&
      s.at(1).z == 2021 && s.front().z == 2020 && s.back().z == 2021) {
    cxx_test_suite_set_correct();
  }
}

void c_take_slice_shared_sort(rust::Slice<Shared> s) {
  // Exercise requirements of RandomAccessIterator.
  // https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
  std::sort(s.begin(), s.end());
  if (s[0].z == 0 && s[1].z == 2 && s[2].z == 4 && s[3].z == 7) {
    cxx_test_suite_set_correct();
  }
}

void c_take_slice_r(rust::Slice<const R> s) {
  if (s.size() == 3 && s[0].get() == 2020 && s[1].get() == 2050) {
    cxx_test_suite_set_correct();
  }
}

bool operator<(const R &a, const R &b) noexcept { return a.get() < b.get(); }

void c_take_slice_r_sort(rust::Slice<R> s) {
  std::qsort(s.data(), s.size(), rust::size_of<decltype(s)::value_type>(),
             [](const void *fst, const void *snd) {
               auto &a = *static_cast<const R *>(fst);
               auto &b = *static_cast<const R *>(snd);
               return a < b ? -1 : b < a ? 1 : 0;
             });
  if (s[0].get() == 2020 && s[1].get() == 2021 && s[2].get() == 2050) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_string(rust::String s) {
  if (std::string(s) == "2020") {
    cxx_test_suite_set_correct();
  }
}

void c_take_unique_ptr_string(std::unique_ptr<std::string> s) {
  if (*s == "2020") {
    cxx_test_suite_set_correct();
  }
}

void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v) {
  if (v->size() == 3) {
    cxx_test_suite_set_correct();
  }
}

void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
  if (v->size() == 5) {
    cxx_test_suite_set_correct();
  }
}

void c_take_unique_ptr_vector_string(
    std::unique_ptr<std::vector<std::string>> v) {
  (void)v;
  cxx_test_suite_set_correct();
}

void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
  if (v->size() == 3) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ref_vector(const std::vector<uint8_t> &v) {
  if (v.size() == 4) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec(rust::Vec<uint8_t> v) { c_take_ref_rust_vec(v); }

void c_take_rust_vec_index(rust::Vec<uint8_t> v) {
  try {
    v.at(100);
  } catch (const std::out_of_range &ex) {
    std::string expected = "rust::Vec index out of range";
    if (ex.what() == expected) {
      cxx_test_suite_set_correct();
    }
  }
}

void c_take_rust_vec_shared(rust::Vec<Shared> v) {
  uint32_t sum = 0;
  for (auto i : v) {
    sum += i.z;
  }
  if (sum == 2021) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec_ns_shared(rust::Vec<::A::AShared> v) {
  uint32_t sum = 0;
  for (auto i : v) {
    sum += i.z;
  }
  if (sum == 2021) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec_nested_ns_shared(rust::Vec<::A::B::ABShared> v) {
  uint32_t sum = 0;
  for (auto i : v) {
    sum += i.z;
  }
  if (sum == 2021) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec_string(rust::Vec<rust::String> v) {
  (void)v;
  cxx_test_suite_set_correct();
}

void c_take_rust_vec_shared_forward_iterator(rust::Vec<Shared> v) {
  // Exercise requirements of ForwardIterator
  // https://en.cppreference.com/w/cpp/named_req/ForwardIterator
  uint32_t sum = 0, csum = 0;
  for (auto it = v.begin(), it_end = v.end(); it != it_end; it++) {
    sum += it->z;
  }
  for (auto it = v.cbegin(), it_end = v.cend(); it != it_end; it++) {
    csum += it->z;
  }
  if (sum == 2021 && csum == 2021) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec_shared_sort(rust::Vec<Shared> v) {
  // Exercise requirements of RandomAccessIterator.
  // https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
  std::sort(v.begin(), v.end());
  if (v[0].z == 0 && v[1].z == 2 && v[2].z == 4 && v[3].z == 7) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec_shared_index(rust::Vec<Shared> v) {
  if (v[0].z == 1010 && v.at(0).z == 1010 && v.front().z == 1010 &&
      v[1].z == 1011 && v.at(1).z == 1011 && v.back().z == 1011) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec_shared_push(rust::Vec<Shared> v) {
  v.push_back(Shared{3});
  v.emplace_back(Shared{2});
  if (v[v.size() - 2].z == 3 && v.back().z == 2) {
    cxx_test_suite_set_correct();
  }
}

void c_take_rust_vec_shared_truncate(rust::Vec<Shared> v) {
  v.truncate(1);
  if (v.size() == 1) {
    v.truncate(0);
    if (v.size() == 0) {
      cxx_test_suite_set_correct();
    }
  }
}

void c_take_rust_vec_shared_clear(rust::Vec<Shared> v) {
  v.clear();
  if (v.size() == 0) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ref_rust_vec(const rust::Vec<uint8_t> &v) {
  uint8_t sum = std::accumulate(v.begin(), v.end(), 0);
  if (sum == 200) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ref_rust_vec_string(const rust::Vec<rust::String> &v) {
  (void)v;
  cxx_test_suite_set_correct();
}

void c_take_ref_rust_vec_index(const rust::Vec<uint8_t> &v) {
  if (v[0] == 86 && v.at(0) == 86 && v.front() == 86 && v[1] == 75 &&
      v.at(1) == 75 && v[3] == 9 && v.at(3) == 9 && v.back() == 9) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ref_rust_vec_copy(const rust::Vec<uint8_t> &v) {
  // The std::copy() will make sure rust::Vec<>::const_iterator satisfies the
  // requirements for std::iterator_traits.
  // https://en.cppreference.com/w/cpp/iterator/iterator_traits
  std::vector<uint8_t> stdv;
  std::copy(v.begin(), v.end(), std::back_inserter(stdv));
  uint8_t sum = std::accumulate(stdv.begin(), stdv.end(), 0);
  if (sum == 200) {
    cxx_test_suite_set_correct();
  }
}

const SharedString &c_take_ref_shared_string(const SharedString &s) {
  if (std::string(s.msg) == "2020") {
    cxx_test_suite_set_correct();
  }
  return s;
}

void c_take_callback(rust::Fn<size_t(rust::String)> callback) {
  callback("2020");
}

void c_take_callback_ref(rust::Fn<void(const rust::String &)> callback) {
  const rust::String string = "2020";
  callback(string);
}

void c_take_callback_mut(rust::Fn<void(rust::String &)> callback) {
  rust::String string = "2020";
  callback(string);
}

void c_take_enum(Enum e) {
  if (e == Enum::AVal) {
    cxx_test_suite_set_correct();
  }
}

void c_take_ns_enum(::A::AEnum e) {
  if (e == ::A::AEnum::AAVal) {
    cxx_test_suite_set_correct();
  }
}

void c_take_nested_ns_enum(::A::B::ABEnum e) {
  if (e == ::A::B::ABEnum::ABAVal) {
    cxx_test_suite_set_correct();
  }
}

size_t c_take_const_ptr(const C *c) { return c->get(); }

size_t c_take_mut_ptr(C *c) {
  size_t result = c->get();
  delete c;
  return result;
}

void c_try_return_void() {}

size_t c_try_return_primitive() { return 2020; }

size_t c_fail_return_primitive() { throw std::logic_error("logic error"); }

rust::Box<R> c_try_return_box() { return c_return_box(); }

const rust::String &c_try_return_ref(const rust::String &s) { return s; }

rust::Str c_try_return_str(rust::Str s) { return s; }

rust::Slice<const uint8_t> c_try_return_sliceu8(rust::Slice<const uint8_t> s) {
  return s;
}

rust::Slice<uint8_t> c_try_return_mutsliceu8(rust::Slice<uint8_t> s) {
  return s;
}

rust::String c_try_return_rust_string() { return c_return_rust_string(); }

std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
  return c_return_unique_ptr_string();
}

rust::Vec<uint8_t> c_try_return_rust_vec() {
  throw std::runtime_error("unimplemented");
}

rust::Vec<rust::String> c_try_return_rust_vec_string() {
  throw std::runtime_error("unimplemented");
}

const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c) {
  (void)c;
  throw std::runtime_error("unimplemented");
}

size_t c_get_use_count(const std::weak_ptr<C> &weak) noexcept {
  return weak.use_count();
}

extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
  return std::unique_ptr<C>(new C{2020}).release();
}

extern "C" void
cxx_test_suite_get_shared_ptr(std::shared_ptr<C> *repr) noexcept {
  new (repr) std::shared_ptr<C>(new C{2020});
}

extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
  return std::unique_ptr<std::string>(new std::string("2020")).release();
}

rust::String C::cOverloadedMethod(int32_t x) const {
  return rust::String(std::to_string(x));
}

rust::String C::cOverloadedMethod(rust::Str x) const {
  return rust::String(std::string(x));
}

rust::String cOverloadedFunction(int x) {
  return rust::String(std::to_string(x));
}

rust::String cOverloadedFunction(rust::Str x) {
  return rust::String(std::string(x));
}

void c_take_trivial_ptr(std::unique_ptr<D> d) {
  if (d->d == 30) {
    cxx_test_suite_set_correct();
  }
}

void c_take_trivial_ref(const D &d) {
  if (d.d == 30) {
    cxx_test_suite_set_correct();
  }
}

void c_take_trivial_mut_ref(D &d) { (void)d; }

void c_take_trivial_pin_ref(const D &d) { (void)d; }

void c_take_trivial_pin_mut_ref(D &d) { (void)d; }

void D::c_take_trivial_ref_method() const {
  if (d == 30) {
    cxx_test_suite_set_correct();
  }
}

void D::c_take_trivial_mut_ref_method() {
  if (d == 30) {
    cxx_test_suite_set_correct();
  }
}

void c_take_trivial(D d) {
  if (d.d == 30) {
    cxx_test_suite_set_correct();
  }
}

void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
  if (g->g == 30) {
    cxx_test_suite_set_correct();
  }
}

void c_take_trivial_ns_ref(const ::G::G &g) {
  if (g.g == 30) {
    cxx_test_suite_set_correct();
  }
}

void c_take_trivial_ns(::G::G g) {
  if (g.g == 30) {
    cxx_test_suite_set_correct();
  }
}

void c_take_opaque_ptr(std::unique_ptr<E> e) {
  if (e->e == 40) {
    cxx_test_suite_set_correct();
  }
}

void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
  if (f->f == 40) {
    cxx_test_suite_set_correct();
  }
}

void c_take_opaque_ref(const E &e) {
  if (e.e == 40 && e.e_str == "hello") {
    cxx_test_suite_set_correct();
  }
}

void E::c_take_opaque_ref_method() const {
  if (e == 40 && e_str == "hello") {
    cxx_test_suite_set_correct();
  }
}

void E::c_take_opaque_mut_ref_method() {
  if (e == 40 && e_str == "hello") {
    cxx_test_suite_set_correct();
  }
}

void c_take_opaque_ns_ref(const ::F::F &f) {
  if (f.f == 40 && f.f_str == "hello") {
    cxx_test_suite_set_correct();
  }
}

std::unique_ptr<D> c_return_trivial_ptr() {
  auto d = std::unique_ptr<D>(new D());
  d->d = 30;
  return d;
}

D c_return_trivial() {
  D d;
  d.d = 30;
  return d;
}

std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
  auto g = std::unique_ptr<::G::G>(new ::G::G());
  g->g = 30;
  return g;
}

::G::G c_return_trivial_ns() {
  ::G::G g;
  g.g = 30;
  return g;
}

std::unique_ptr<E> c_return_opaque_ptr() {
  auto e = std::unique_ptr<E>(new E());
  e->e = 40;
  e->e_str = std::string("hello");
  return e;
}

E &c_return_opaque_mut_pin(E &e) { return e; }

std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
  auto f = std::unique_ptr<::F::F>(new ::F::F());
  f->f = 40;
  f->f_str = std::string("hello");
  return f;
}

extern "C" const char *cxx_run_test() noexcept {
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define ASSERT(x)                                                              \
  do {                                                                         \
    if (!(x)) {                                                                \
      return "Assertion failed: `" #x "`, " __FILE__ ":" TOSTRING(__LINE__);   \
    }                                                                          \
  } while (false)

  ASSERT(rust::size_of<R>() == sizeof(size_t));
  ASSERT(rust::align_of<R>() == alignof(size_t));
  ASSERT(rust::size_of<size_t>() == sizeof(size_t));
  ASSERT(rust::align_of<size_t>() == alignof(size_t));

  ASSERT(r_return_primitive() == 2020);
  ASSERT(r_return_shared().z == 2020);
  ASSERT(cxx_test_suite_r_is_correct(&*r_return_box()));
  ASSERT(r_return_unique_ptr()->get() == 2020);
  ASSERT(r_return_shared_ptr()->get() == 2020);
  ASSERT(r_return_ref(Shared{2020}) == 2020);
  ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
  ASSERT(std::string(r_return_rust_string()) == "2020");
  ASSERT(*r_return_unique_ptr_string() == "2020");
  ASSERT(r_return_identity(2020) == 2020);
  ASSERT(r_return_sum(2020, 1) == 2021);
  ASSERT(r_return_enum(0) == Enum::AVal);
  ASSERT(r_return_enum(1) == Enum::BVal);
  ASSERT(r_return_enum(2021) == Enum::CVal);

  r_take_primitive(2020);
  r_take_shared(Shared{2020});
  r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
  r_take_shared_ptr(std::shared_ptr<C>(new C{2020}));
  r_take_ref_c(C{2020});
  r_take_str(rust::Str("2020"));
  r_take_slice_char(rust::Slice<const char>(SLICE_DATA, sizeof(SLICE_DATA)));
  r_take_rust_string(rust::String("2020"));
  r_take_unique_ptr_string(
      std::unique_ptr<std::string>(new std::string("2020")));
  r_take_ref_vector(std::vector<uint8_t>{20, 2, 0});
  std::vector<uint64_t> empty_vector;
  r_take_ref_empty_vector(empty_vector);
  empty_vector.reserve(10);
  r_take_ref_empty_vector(empty_vector);
  r_take_enum(Enum::AVal);

  ASSERT(r_try_return_primitive() == 2020);
  try {
    r_fail_return_primitive();
    ASSERT(false);
  } catch (const rust::Error &e) {
    ASSERT(std::strcmp(e.what(), "rust error") == 0);
  }

  auto r = r_return_box();
  ASSERT(r->get() == 2020);
  ASSERT(r->set(2021) == 2021);
  ASSERT(r->get() == 2021);

  using std::swap;
  auto r2 = r_return_box();
  swap(r, r2);
  ASSERT(r->get() == 2020);
  ASSERT(r2->get() == 2021);

  ASSERT(std::string(Shared{0}.r_method_on_shared()) == "2020");

  ASSERT(std::string(rAliasedFunction(2020)) == "2020");

  ASSERT(Shared{1} == Shared{1});
  ASSERT(Shared{1} != Shared{2});

  rust::String first = "first", second = "second", sec = "sec";
  bool (rust::String::*cmp)(const rust::String &) const;
  bool first_first, first_second, sec_second, second_sec;
  for (auto test : {
           std::tuple<decltype(cmp), bool, bool, bool, bool>{
               &rust::String::operator==, true, false, false, false},
           {&rust::String::operator!=, false, true, true, true},
           {&rust::String::operator<, false, true, true, false},
           {&rust::String::operator<=, true, true, true, false},
           {&rust::String::operator>, false, false, false, true},
           {&rust::String::operator>=, true, false, false, true},
       }) {
    std::tie(cmp, first_first, first_second, sec_second, second_sec) = test;
    ASSERT((first.*cmp)(first) == first_first);
    ASSERT((first.*cmp)(second) == first_second);
    ASSERT((sec.*cmp)(second) == sec_second);
    ASSERT((second.*cmp)(sec) == second_sec);
  }

  rust::String cstring = "test";
  ASSERT(cstring.length() == 4);
  ASSERT(strncmp(cstring.data(), "test", 4) == 0);
  ASSERT(strncmp(cstring.c_str(), "test", 5) == 0);
  ASSERT(cstring.length() == 4);

  rust::String other_cstring = "foo";
  swap(cstring, other_cstring);
  ASSERT(cstring == "foo");
  ASSERT(other_cstring == "test");

  ASSERT(cstring.capacity() == 3);
  cstring.reserve(2);
  ASSERT(cstring.capacity() == 3);
  cstring.reserve(5);
  ASSERT(cstring.capacity() >= 5);

  rust::Str cstr = "test";
  rust::Str other_cstr = "foo";
  swap(cstr, other_cstr);
  ASSERT(cstr == "foo");
  ASSERT(other_cstr == "test");

  const char *utf8_literal = u8"Test string";
  const char16_t *utf16_literal = u"Test string";
  rust::String utf8_rstring = utf8_literal;
  rust::String utf16_rstring = utf16_literal;
  ASSERT(utf8_rstring == utf16_rstring);

  const char *bad_utf8_literal = "test\x80";
  const char16_t *bad_utf16_literal = u"test\xDD1E";
  rust::String bad_utf8_rstring = rust::String::lossy(bad_utf8_literal);
  rust::String bad_utf16_rstring = rust::String::lossy(bad_utf16_literal);
  ASSERT(bad_utf8_rstring == bad_utf16_rstring);

  rust::Vec<int> vec1{1, 2};
  rust::Vec<int> vec2{3, 4};
  swap(vec1, vec2);
  ASSERT(vec1[0] == 3 && vec1[1] == 4);
  ASSERT(vec2[0] == 1 && vec2[1] == 2);

  // Test Vec<usize> and Vec<isize>. These are weird because on Linux and
  // Windows size_t is exactly the same C++ type as one of the sized integer
  // types (typically uint64_t, both of which are defined as unsigned long),
  // while on macOS it is a distinct type.
  // https://github.com/dtolnay/cxx/issues/705
  (void)rust::Vec<size_t>();
  (void)rust::Vec<rust::isize>();

  cxx_test_suite_set_correct();
  return nullptr;
}

} // namespace tests

namespace other {
void ns_c_take_trivial(::tests::D d) {
  if (d.d == 30) {
    cxx_test_suite_set_correct();
  }
}

::tests::D ns_c_return_trivial() {
  ::tests::D d;
  d.d = 30;
  return d;
}

void ns_c_take_ns_shared(::A::AShared shared) {
  if (shared.z == 2020) {
    cxx_test_suite_set_correct();
  }
}
} // namespace other

namespace I {
uint32_t I::get() const { return a; }

std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
  return std::unique_ptr<I>(new I());
}
} // namespace I

// Instantiate any remaining class member functions not already covered above.
// This is an easy way to at least typecheck anything missed by unit tests.
// https://en.cppreference.com/w/cpp/language/class_template#Explicit_instantiation
// > When an explicit instantiation names a class template specialization, it
// > serves as an explicit instantiation of the same kind (declaration or
// > definition) of each of its non-inherited non-template members that has not
// > been previously explicitly specialized in the translation unit.
#if defined(CXX_TEST_INSTANTIATIONS)
template class rust::Box<tests::Shared>;
template class rust::Slice<const char>;
template class rust::Slice<const uint8_t>;
template class rust::Slice<uint8_t>;
template class rust::Slice<const tests::Shared>;
template class rust::Slice<tests::Shared>;
template class rust::Slice<const tests::R>;
template class rust::Slice<tests::R>;
template class rust::Vec<uint8_t>;
template class rust::Vec<rust::String>;
template class rust::Vec<tests::Shared>;
template class rust::Fn<size_t(rust::String)>;
#endif