Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1 | /* 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 Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 17 | #include <gtest/gtest.h> |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 18 | |
| 19 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 20 | TEST(RefCountTest, Basic) { |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 21 | CRYPTO_refcount_t count = 0; |
| 22 | |
| 23 | CRYPTO_refcount_inc(&count); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 24 | EXPECT_EQ(1u, count); |
| 25 | |
| 26 | EXPECT_TRUE(CRYPTO_refcount_dec_and_test_zero(&count)); |
| 27 | EXPECT_EQ(0u, count); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 28 | |
| 29 | count = CRYPTO_REFCOUNT_MAX; |
| 30 | CRYPTO_refcount_inc(&count); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 31 | 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 Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 36 | |
| 37 | count = 2; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 38 | EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count)); |
| 39 | EXPECT_EQ(1u, count); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 40 | } |