File: multi_token_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (183 lines) | stat: -rw-r--r-- 5,755 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/tokens/multi_token.h"

#include <algorithm>
#include <variant>

#include "base/types/token_type.h"
#include "base/unguessable_token.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blink {

using FooToken = base::TokenType<class FooTokenTag>;
using BarToken = base::TokenType<class BarTokenTag>;
using BazToken = base::TokenType<class BazTokenTag>;

static_assert(internal::IsBaseToken<FooToken>);
static_assert(!internal::IsBaseToken<int>);

static_assert(internal::AreAllUnique<int>);
static_assert(!internal::AreAllUnique<int, int>);
static_assert(!internal::AreAllUnique<int, char, int>);

static_assert(internal::IsCompatible<BazToken, FooToken, BarToken, BazToken>);
static_assert(!internal::IsCompatible<BazToken, FooToken, BarToken>);

using FooBarToken = MultiToken<FooToken, BarToken>;
using FooBarBazToken = MultiToken<FooToken, BarToken, BazToken>;

static_assert(FooBarBazToken::IndexOf<FooToken>() == FooBarBazToken::Tag{0});
static_assert(FooBarBazToken::IndexOf<BarToken>() == FooBarBazToken::Tag{1});
static_assert(FooBarBazToken::IndexOf<BazToken>() == FooBarBazToken::Tag{2});

TEST(MultiTokenTest, MultiTokenWorks) {
  // Test default initialization.
  FooBarToken token1;
  EXPECT_FALSE(token1.value().is_empty());
  EXPECT_EQ(FooBarToken::Tag{0}, token1.variant_index());
  EXPECT_TRUE(token1.Is<FooToken>());
  EXPECT_FALSE(token1.Is<BarToken>());

  // Test copy construction.
  BarToken bar = BarToken();
  FooBarToken token2(bar);
  EXPECT_EQ(token2.value(), bar.value());
  EXPECT_FALSE(token2.value().is_empty());
  EXPECT_EQ(FooBarToken::Tag{1}, token2.variant_index());
  EXPECT_FALSE(token2.Is<FooToken>());
  EXPECT_TRUE(token2.Is<BarToken>());

  // Test assignment.
  FooBarToken token3;
  token3 = token2;
  EXPECT_EQ(token3.value(), token2.value());
  EXPECT_FALSE(token3.value().is_empty());
  EXPECT_EQ(token2.variant_index(), token3.variant_index());
  EXPECT_FALSE(token3.Is<FooToken>());
  EXPECT_TRUE(token3.Is<BarToken>());

  // Test hasher.
  EXPECT_EQ(FooBarToken::Hasher()(token2),
            base::UnguessableTokenHash()(token2.value()));

  // Test string representation.
  EXPECT_EQ(token2.ToString(), token2.value().ToString());

  // Test type conversions.
  FooToken foo(token1.value());
  EXPECT_EQ(foo, token1.GetAs<FooToken>());
  EXPECT_EQ(token2.GetAs<BarToken>(), token3.GetAs<BarToken>());
}

TEST(MultiTokenTest, Comparison) {
  // Tests comparisons between:
  // - two multi tokens that hold different types and underlying values
  // - two multi tokens that hold the same type and underlying value
  {
    FooBarToken token1 = FooToken();
    FooBarToken token2 = BarToken();
    FooBarToken token3 = token2;

    EXPECT_FALSE(token1 == token2);
    EXPECT_TRUE(token1 != token2);
    EXPECT_TRUE(token2 == token3);
    EXPECT_FALSE(token2 != token3);

    // std::variant orders by index. If the indices are equal (e.g. the same
    // type is held in both), then the comparison operator of the held type is
    // used.
    EXPECT_TRUE(token1 < token2);
    EXPECT_TRUE(token1 < token3);
    EXPECT_FALSE(token2 < token3);

    EXPECT_TRUE(token1 <= token2);
    EXPECT_TRUE(token1 <= token3);
    EXPECT_TRUE(token2 <= token3);
    EXPECT_TRUE(token3 <= token2);

    EXPECT_FALSE(token1 > token2);
    EXPECT_FALSE(token1 > token3);
    EXPECT_FALSE(token2 > token3);

    EXPECT_FALSE(token1 >= token2);
    EXPECT_FALSE(token1 >= token3);
    EXPECT_TRUE(token2 >= token3);
    EXPECT_TRUE(token3 >= token2);
  }

  // Tests comparisons between two multi tokens that hold the same type but
  // different underlying values.
  {
    // Necessary because std::minmax() returns a pair of references.
    FooToken foo1;
    FooToken foo2;
    const auto& [lesser, greater] = std::minmax(foo1, foo2);
    FooBarToken token1 = lesser;
    FooBarToken token2 = greater;

    EXPECT_FALSE(token1 == token2);
    EXPECT_TRUE(token1 != token2);

    EXPECT_TRUE(token1 < token2);
    EXPECT_FALSE(token2 < token1);

    EXPECT_FALSE(token1 > token2);
    EXPECT_TRUE(token2 > token1);

    EXPECT_TRUE(token1 <= token2);
    EXPECT_FALSE(token2 <= token1);

    EXPECT_FALSE(token1 >= token2);
    EXPECT_TRUE(token2 >= token1);
  }
}

TEST(MultiTokenTest, Visit) {
  struct Visitor {
    std::string_view operator()(const FooToken& token) { return "FooToken"; }
    std::string_view operator()(const BarToken& token) { return "BarToken"; }
    std::string_view operator()(const BazToken& token) { return "BazToken"; }
  };

  FooBarBazToken token(FooToken{});
  EXPECT_EQ(token.Visit(Visitor()), "FooToken");

  token = BarToken{};
  EXPECT_EQ(token.Visit(Visitor()), "BarToken");

  token = BazToken{};
  EXPECT_EQ(token.Visit(Visitor()), "BazToken");
}

TEST(MultiTokenTest, CompatibleConstruction) {
  {
    FooBarToken foo_bar_token(FooToken{});
    FooBarBazToken foo_bar_baz_token(foo_bar_token);
    EXPECT_EQ(FooBarBazToken::Tag{0}, foo_bar_baz_token.variant_index());
  }
  {
    FooBarToken foo_bar_token(BarToken{});
    FooBarBazToken foo_bar_baz_token(foo_bar_token);
    EXPECT_EQ(FooBarBazToken::Tag{1}, foo_bar_baz_token.variant_index());
  }
}

TEST(MultiTokenTest, CompatibleAssignment) {
  FooBarBazToken foo_bar_baz_token;
  {
    FooBarToken foo_bar_token(FooToken{});
    foo_bar_baz_token = foo_bar_token;
    EXPECT_EQ(FooBarBazToken::Tag{0}, foo_bar_baz_token.variant_index());
  }
  {
    FooBarToken foo_bar_token(BarToken{});
    foo_bar_baz_token = foo_bar_token;
    EXPECT_EQ(FooBarBazToken::Tag{1}, foo_bar_baz_token.variant_index());
  }
}

}  // namespace blink