Dragos Sbirlea | 8cc5162 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "scoped_hashtable.h" |
| 18 | |
| 19 | #include "common_runtime_test.h" |
Dragos Sbirlea | 8cc5162 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 20 | |
| 21 | using utils::ScopedHashtable; |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class Value { |
| 26 | public: |
| 27 | explicit Value(int v):value_(v) {} |
| 28 | int value_; |
| 29 | }; |
| 30 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 31 | class ScopedHashtableTest : public testing::Test {}; |
Dragos Sbirlea | 8cc5162 | 2013-06-21 09:20:34 -0700 | [diff] [blame] | 32 | |
| 33 | TEST_F(ScopedHashtableTest, Basics) { |
| 34 | ScopedHashtable<int, Value*> sht; |
| 35 | // Check table is empty when no scope is open. |
| 36 | EXPECT_TRUE(NULL == sht.Lookup(1)); |
| 37 | |
| 38 | // Check table is empty when scope open. |
| 39 | sht.OpenScope(); |
| 40 | EXPECT_TRUE(NULL == sht.Lookup(1)); |
| 41 | // Check table is empty after closing scope. |
| 42 | EXPECT_EQ(sht.CloseScope(), true); |
| 43 | // Check closing scope on empty table is no-op. |
| 44 | EXPECT_EQ(sht.CloseScope(), false); |
| 45 | // Check that find in current scope works. |
| 46 | sht.OpenScope(); |
| 47 | sht.Add(1, new Value(1)); |
| 48 | EXPECT_EQ(sht.Lookup(1)->value_, 1); |
| 49 | // Check that updating values in current scope works. |
| 50 | sht.Add(1, new Value(2)); |
| 51 | EXPECT_EQ(sht.Lookup(1)->value_, 2); |
| 52 | // Check that find works in previous scope. |
| 53 | sht.OpenScope(); |
| 54 | EXPECT_EQ(sht.Lookup(1)->value_, 2); |
| 55 | // Check that shadowing scopes works. |
| 56 | sht.Add(1, new Value(3)); |
| 57 | EXPECT_EQ(sht.Lookup(1)->value_, 3); |
| 58 | // Check that having multiple keys work correctly. |
| 59 | sht.Add(2, new Value(4)); |
| 60 | EXPECT_EQ(sht.Lookup(1)->value_, 3); |
| 61 | EXPECT_EQ(sht.Lookup(2)->value_, 4); |
| 62 | // Check that scope removal works corectly. |
| 63 | sht.CloseScope(); |
| 64 | EXPECT_EQ(sht.Lookup(1)->value_, 2); |
| 65 | EXPECT_TRUE(NULL == sht.Lookup(2)); |
| 66 | } |
| 67 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 68 | } // namespace art |