blob: efa501a7e792649f67e6774cd363cc051e557c07 [file] [log] [blame]
Adam Langleyf4e42722015-06-04 17:45:09 -07001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include "internal.h"
16
Robert Sloan8ff03552017-06-14 12:40:58 -070017#include <gtest/gtest.h>
Adam Langleyf4e42722015-06-04 17:45:09 -070018
19
Robert Sloan8ff03552017-06-14 12:40:58 -070020TEST(RefCountTest, Basic) {
Adam Langleyf4e42722015-06-04 17:45:09 -070021 CRYPTO_refcount_t count = 0;
22
23 CRYPTO_refcount_inc(&count);
Robert Sloan8ff03552017-06-14 12:40:58 -070024 EXPECT_EQ(1u, count);
25
26 EXPECT_TRUE(CRYPTO_refcount_dec_and_test_zero(&count));
27 EXPECT_EQ(0u, count);
Adam Langleyf4e42722015-06-04 17:45:09 -070028
29 count = CRYPTO_REFCOUNT_MAX;
30 CRYPTO_refcount_inc(&count);
Robert Sloan8ff03552017-06-14 12:40:58 -070031 EXPECT_EQ(CRYPTO_REFCOUNT_MAX, count)
32 << "Count did not saturate correctly when incrementing.";
33 EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count));
34 EXPECT_EQ(CRYPTO_REFCOUNT_MAX, count)
35 << "Count did not saturate correctly when decrementing.";
Adam Langleyf4e42722015-06-04 17:45:09 -070036
37 count = 2;
Robert Sloan8ff03552017-06-14 12:40:58 -070038 EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count));
39 EXPECT_EQ(1u, count);
Adam Langleyf4e42722015-06-04 17:45:09 -070040}