Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "utils.h"
     18 
     19 #include "gtest/gtest.h"
     20 
     21 namespace art {
     22 
     23 class UtilsTest : public testing::Test {};
     24 
     25 TEST_F(UtilsTest, PrettySize) {
     26   EXPECT_EQ("1GB", PrettySize(1 * GB));
     27   EXPECT_EQ("2GB", PrettySize(2 * GB));
     28   if (sizeof(size_t) > sizeof(uint32_t)) {
     29     EXPECT_EQ("100GB", PrettySize(100 * GB));
     30   }
     31   EXPECT_EQ("1024KB", PrettySize(1 * MB));
     32   EXPECT_EQ("10MB", PrettySize(10 * MB));
     33   EXPECT_EQ("100MB", PrettySize(100 * MB));
     34   EXPECT_EQ("1024B", PrettySize(1 * KB));
     35   EXPECT_EQ("10KB", PrettySize(10 * KB));
     36   EXPECT_EQ("100KB", PrettySize(100 * KB));
     37   EXPECT_EQ("0B", PrettySize(0));
     38   EXPECT_EQ("1B", PrettySize(1));
     39   EXPECT_EQ("10B", PrettySize(10));
     40   EXPECT_EQ("100B", PrettySize(100));
     41   EXPECT_EQ("512B", PrettySize(512));
     42 }
     43 
     44 TEST_F(UtilsTest, Split) {
     45   std::vector<std::string> actual;
     46   std::vector<std::string> expected;
     47 
     48   expected.clear();
     49 
     50   actual.clear();
     51   Split("", ':', &actual);
     52   EXPECT_EQ(expected, actual);
     53 
     54   actual.clear();
     55   Split(":", ':', &actual);
     56   EXPECT_EQ(expected, actual);
     57 
     58   expected.clear();
     59   expected.push_back("foo");
     60 
     61   actual.clear();
     62   Split(":foo", ':', &actual);
     63   EXPECT_EQ(expected, actual);
     64 
     65   actual.clear();
     66   Split("foo:", ':', &actual);
     67   EXPECT_EQ(expected, actual);
     68 
     69   actual.clear();
     70   Split(":foo:", ':', &actual);
     71   EXPECT_EQ(expected, actual);
     72 
     73   expected.push_back("bar");
     74 
     75   actual.clear();
     76   Split("foo:bar", ':', &actual);
     77   EXPECT_EQ(expected, actual);
     78 
     79   actual.clear();
     80   Split(":foo:bar", ':', &actual);
     81   EXPECT_EQ(expected, actual);
     82 
     83   actual.clear();
     84   Split("foo:bar:", ':', &actual);
     85   EXPECT_EQ(expected, actual);
     86 
     87   actual.clear();
     88   Split(":foo:bar:", ':', &actual);
     89   EXPECT_EQ(expected, actual);
     90 
     91   expected.push_back("baz");
     92 
     93   actual.clear();
     94   Split("foo:bar:baz", ':', &actual);
     95   EXPECT_EQ(expected, actual);
     96 
     97   actual.clear();
     98   Split(":foo:bar:baz", ':', &actual);
     99   EXPECT_EQ(expected, actual);
    100 
    101   actual.clear();
    102   Split("foo:bar:baz:", ':', &actual);
    103   EXPECT_EQ(expected, actual);
    104 
    105   actual.clear();
    106   Split(":foo:bar:baz:", ':', &actual);
    107   EXPECT_EQ(expected, actual);
    108 }
    109 
    110 TEST_F(UtilsTest, ArrayCount) {
    111   int i[64];
    112   EXPECT_EQ(ArrayCount(i), 64u);
    113   char c[7];
    114   EXPECT_EQ(ArrayCount(c), 7u);
    115 }
    116 
    117 TEST_F(UtilsTest, BoundsCheckedCast) {
    118   char buffer[64];
    119   const char* buffer_end = buffer + ArrayCount(buffer);
    120   EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(nullptr, buffer, buffer_end), nullptr);
    121   EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer, buffer, buffer_end),
    122             reinterpret_cast<const uint64_t*>(buffer));
    123   EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 56, buffer, buffer_end),
    124             reinterpret_cast<const uint64_t*>(buffer + 56));
    125   EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer - 1, buffer, buffer_end), nullptr);
    126   EXPECT_EQ(BoundsCheckedCast<const uint64_t*>(buffer + 57, buffer, buffer_end), nullptr);
    127 }
    128 
    129 }  // namespace art
    130