File: test_tagged_union5.cpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (69 lines) | stat: -rw-r--r-- 1,405 bytes parent folder | download | duplicates (2)
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
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>

#include <gtest/gtest.h>

namespace test_tagged_union5 {


  struct Sa {
    int a;
    std::string b;
  };

  struct Sb {
    std::string b;
    int i;
  };

  template <typename T, typename... Ts>
  concept OneOf = (std::is_same_v<T, Ts> || ...);

	template <typename T>
	bool operator==(const T& lhs, const T& rhs)
    requires OneOf<T,
                   struct test_tagged_union5::Sa,
                   struct test_tagged_union5::Sb>
	{
		return rfl::to_named_tuple(lhs) == rfl::to_named_tuple(rhs);
	}


  using Tu = rfl::TaggedUnion<"tu",Sa,Sb>;

  Tu v1 = Sa{.a = 1, .b = "b" };
  Tu v2 = Sa{.a = 1, .b = "b" };
  Tu v3 = Sa{.a = 1, .b = "bc" };
  Tu v4 = Sb{.b = "s", .i = -2};

  TEST(json, test_tagged_union5) {
    ASSERT_TRUE(v1 == v1);
    ASSERT_TRUE(v1 == v2);
    ASSERT_TRUE(v1 != v3);
    ASSERT_TRUE(v1 != v4);

    ASSERT_TRUE(v2 == v2);
    ASSERT_TRUE(v2 != v3);
    ASSERT_TRUE(v2 != v4);
    ASSERT_TRUE(v2 == v1);

    ASSERT_TRUE(v3 == v3);
    ASSERT_TRUE(v3 != v4);
    ASSERT_TRUE(v3 != v1);
    ASSERT_TRUE(v3 != v2);

    ASSERT_TRUE(v4 == v4);
    ASSERT_TRUE(v4 != v1);
    ASSERT_TRUE(v4 != v2);
    ASSERT_TRUE(v4 != v3);

    ASSERT_FALSE(v4 == v2);
    // This is a compile-time test
    EXPECT_TRUE(true);
  }

}  // namespace test_tagged_union5