safemap_test.go 2.1 KB

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