Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: base/nonce_unittest.cc

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Removed '= default' constructor. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/nonce.h"
6
7 #include <type_traits>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12
13 void TestSmallerThanOperator(const Nonce& a, const Nonce& b) {
14 EXPECT_TRUE(a < b);
15 EXPECT_FALSE(b < a);
16 }
17
18 TEST(NonceTest, VerifyEqualityOperators) {
19 // Deserialize is used for testing purposes.
20 // Use Nonce::Generate() to generate nonces in productions code instead.
21 Nonce nonce = Nonce::Deserialize(1, 2);
22 Nonce same_nonce = Nonce::Deserialize(1, 2);
23 Nonce diff_nonce = Nonce::Deserialize(1, 3);
24
25 EXPECT_TRUE(nonce == nonce);
26 EXPECT_FALSE(nonce != nonce);
27
28 EXPECT_TRUE(nonce == same_nonce);
29 EXPECT_FALSE(nonce != same_nonce);
30
31 EXPECT_FALSE(nonce == diff_nonce);
32 EXPECT_FALSE(diff_nonce == nonce);
33 EXPECT_TRUE(nonce != diff_nonce);
34 EXPECT_TRUE(diff_nonce != nonce);
35 }
36
37 TEST(NonceTest, VerifyConstructors) {
38 Nonce nonce = Nonce::Generate();
39 EXPECT_FALSE(nonce.is_empty());
40
41 EXPECT_EQ(nonce, Nonce::Deserialize(nonce.high(), nonce.low()));
42 EXPECT_EQ(nonce, Nonce(nonce));
43
44 Nonce uninitialized;
45 EXPECT_TRUE(uninitialized.is_empty());
46 EXPECT_TRUE(Nonce().is_empty());
47 }
48
49 TEST(NonceTest, VerifySmallerThanOperator) {
50 // Deserialize is used for testing purposes.
51 // Use Nonce::Generate() to generate nonces in productions code instead.
52
53 // a.low < b.low and a.high == b.high.
54 TestSmallerThanOperator(Nonce::Deserialize(0, 1), Nonce::Deserialize(0, 5));
55
56 // a.low == b.low and a.high < b.high.
57 TestSmallerThanOperator(Nonce::Deserialize(1, 0), Nonce::Deserialize(5, 0));
58
59 // a.low < b.low and a.high < b.high.
60 TestSmallerThanOperator(Nonce::Deserialize(1, 1), Nonce::Deserialize(5, 5));
61
62 // a.low > b.low and a.high < b.high.
63 TestSmallerThanOperator(Nonce::Deserialize(1, 10), Nonce::Deserialize(10, 1));
64 }
65
66 TEST(NonceTest, VerifyHash) {
67 Nonce nonce = Nonce::Generate();
68 EXPECT_EQ(base::HashInts64(nonce.high(), nonce.low()), NonceHash{}(nonce));
69 }
70
71 TEST(NonceTest, VerifyBasicUniqueness) {
72 EXPECT_NE(Nonce::Generate(), Nonce::Generate());
73 }
74 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698