1 // Copyright 2016 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License for more details.
11
12 #include "base/ArraySize.h"
13
14 #include <gtest/gtest.h>
15
16 #include <vector>
17
18 namespace android {
19 namespace base {
20
TEST(ArraySize,Sizes)21 TEST(ArraySize, Sizes) {
22 int array1[100];
23 EXPECT_EQ(100U, arraySize(array1));
24 EXPECT_EQ(100U, ARRAY_SIZE(array1));
25
26 char array2[200];
27 EXPECT_EQ(200U, arraySize(array2));
28 EXPECT_EQ(200U, ARRAY_SIZE(array2));
29
30 std::array<std::vector<bool>, 15> array3;
31 EXPECT_EQ(15U, arraySize(array3));
32 EXPECT_EQ(15U, ARRAY_SIZE(array3));
33 }
34
TEST(ArraySize,CompileTime)35 TEST(ArraySize, CompileTime) {
36 static constexpr int arr[20] = {};
37 static_assert(ARRAY_SIZE(arr) == 20U,
38 "Bad ARRAY_SIZE() result in compile time");
39 static_assert(arraySize(arr) == 20U,
40 "Bad arraySize() result in compile time");
41
42 static constexpr bool arr2[arraySize(arr)] = {};
43 static_assert(arraySize(arr2) == 20U,
44 "Bad size of a new array declared with a result of "
45 "arraySize() call");
46 static_assert(arraySize(arr) == ARRAY_SIZE(arr2),
47 "Macro and function are compatible");
48 }
49
50 } // namespace base
51 } // namespace android
52