Home | History | Annotate | Download | only in common
      1 // Copyright (C) 2014 The Android Open Source Project
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 // http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include "emugl/common/id_to_object_map.h"
     16 
     17 #include <gtest/gtest.h>
     18 
     19 namespace emugl {
     20 
     21 namespace {
     22 
     23 typedef IdToObjectMapBase::KeyType KeyType;
     24 
     25 class Foo {
     26 public:
     27     Foo() : mVal(0) {}
     28     Foo(int val) : mVal(val) {}
     29     ~Foo() {}
     30     int val() const { return mVal; }
     31     void setVal(int val) { mVal = val; }
     32 private:
     33     int mVal;
     34 };
     35 
     36 }  // namespace
     37 
     38 TEST(IdToObjectMap, Empty) {
     39     IdToObjectMap<Foo> map;
     40     EXPECT_TRUE(map.empty());
     41     EXPECT_EQ(0U, map.size());
     42 }
     43 
     44 TEST(IdToObjectMap, SetIntegerRange) {
     45     IdToObjectMap<Foo> map;
     46     KeyType kMax = 10000;
     47 
     48     // Add all items in the map.
     49     for (KeyType n = 0; n < kMax; ++n) {
     50         EXPECT_FALSE(map.set(n, new Foo(n))) << "For key " << n;
     51     }
     52 
     53     // Check final size.
     54     EXPECT_EQ(static_cast<size_t>(kMax), map.size());
     55 
     56     // Find all items in the map.
     57     for (KeyType n = 0; n < kMax; ++n) {
     58         EXPECT_TRUE(map.contains(n)) << "For key " << n;
     59         Foo* foo = NULL;
     60         EXPECT_TRUE(map.find(n, &foo)) << "For key " << n;
     61         if (foo) {
     62             EXPECT_EQ(static_cast<int>(n), foo->val()) << "For key " << n;
     63         }
     64     }
     65 }
     66 
     67 TEST(IdToObjectMap, RemoveAll) {
     68     IdToObjectMap<Foo> map;
     69     KeyType kMax = 10000;
     70 
     71     // Add all items in the map.
     72     for (KeyType n = 0; n < kMax; ++n) {
     73         EXPECT_FALSE(map.set(n, new Foo(n))) << "For key " << n;
     74     }
     75 
     76     EXPECT_EQ(static_cast<size_t>(kMax), map.size());
     77 
     78     for (KeyType n = 0; n < kMax; ++n) {
     79         EXPECT_TRUE(map.remove(n)) << "For key " << n;
     80     }
     81     EXPECT_EQ(0U, map.size());
     82 }
     83 
     84 TEST(IdToObjectMap, RemoveOdd) {
     85     IdToObjectMap<Foo> map;
     86     KeyType kMax = 10000;
     87 
     88     // Add all items in the map.
     89     for (KeyType n = 0; n < kMax; ++n) {
     90         EXPECT_FALSE(map.set(n, new Foo(n))) << "For key " << n;
     91     }
     92 
     93     EXPECT_EQ(static_cast<size_t>(kMax), map.size());
     94 
     95     for (KeyType n = 0; n < kMax; ++n) {
     96         if (n & 1) {
     97             EXPECT_TRUE(map.remove(n)) << "For key " << n;
     98         }
     99     }
    100     EXPECT_EQ(static_cast<size_t>(kMax / 2), map.size());
    101 
    102     for (KeyType n = 0; n < kMax; ++n) {
    103         if (n & 1) {
    104             EXPECT_FALSE(map.contains(n)) << "For key " << n;
    105         } else {
    106             EXPECT_TRUE(map.contains(n)) << "For key " << n;
    107             Foo* foo = NULL;
    108             EXPECT_TRUE(map.find(n, &foo)) << "For key " << n;
    109             if (foo) {
    110                 EXPECT_EQ(static_cast<int>(n), foo->val());
    111             }
    112         }
    113     }
    114 }
    115 
    116 }  // namespace emugl
    117