github.com/astaxie/beego@v1.12.3/utils/safemap_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 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 package utils 16 17 import "testing" 18 19 var safeMap *BeeMap 20 21 func TestNewBeeMap(t *testing.T) { 22 safeMap = NewBeeMap() 23 if safeMap == nil { 24 t.Fatal("expected to return non-nil BeeMap", "got", safeMap) 25 } 26 } 27 28 func TestSet(t *testing.T) { 29 safeMap = NewBeeMap() 30 if ok := safeMap.Set("astaxie", 1); !ok { 31 t.Error("expected", true, "got", false) 32 } 33 } 34 35 func TestReSet(t *testing.T) { 36 safeMap := NewBeeMap() 37 if ok := safeMap.Set("astaxie", 1); !ok { 38 t.Error("expected", true, "got", false) 39 } 40 // set diff value 41 if ok := safeMap.Set("astaxie", -1); !ok { 42 t.Error("expected", true, "got", false) 43 } 44 45 // set same value 46 if ok := safeMap.Set("astaxie", -1); ok { 47 t.Error("expected", false, "got", true) 48 } 49 } 50 51 func TestCheck(t *testing.T) { 52 if exists := safeMap.Check("astaxie"); !exists { 53 t.Error("expected", true, "got", false) 54 } 55 } 56 57 func TestGet(t *testing.T) { 58 if val := safeMap.Get("astaxie"); val.(int) != 1 { 59 t.Error("expected value", 1, "got", val) 60 } 61 } 62 63 func TestDelete(t *testing.T) { 64 safeMap.Delete("astaxie") 65 if exists := safeMap.Check("astaxie"); exists { 66 t.Error("expected element to be deleted") 67 } 68 } 69 70 func TestItems(t *testing.T) { 71 safeMap := NewBeeMap() 72 safeMap.Set("astaxie", "hello") 73 for k, v := range safeMap.Items() { 74 key := k.(string) 75 value := v.(string) 76 if key != "astaxie" { 77 t.Error("expected the key should be astaxie") 78 } 79 if value != "hello" { 80 t.Error("expected the value should be hello") 81 } 82 } 83 } 84 85 func TestCount(t *testing.T) { 86 if count := safeMap.Count(); count != 0 { 87 t.Error("expected count to be", 0, "got", count) 88 } 89 }