You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.3 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestIterateKeysWithValues(t *testing.T) {
  9. cmap := NewCMap()
  10. for i := 1; i <= 10; i++ {
  11. cmap.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
  12. }
  13. // Testing size
  14. assert.Equal(t, 10, cmap.Size())
  15. assert.Equal(t, 10, len(cmap.Keys()))
  16. assert.Equal(t, 10, len(cmap.Values()))
  17. // Iterating Keys, checking for matching Value
  18. for _, key := range cmap.Keys() {
  19. val := strings.Replace(key, "key", "value", -1)
  20. assert.Equal(t, val, cmap.Get(key))
  21. }
  22. // Test if all keys are within []Keys()
  23. keys := cmap.Keys()
  24. for i := 1; i <= 10; i++ {
  25. assert.Contains(t, keys, fmt.Sprintf("key%d", i), "cmap.Keys() should contain key")
  26. }
  27. // Delete 1 Key
  28. cmap.Delete("key1")
  29. assert.NotEqual(t, len(keys), len(cmap.Keys()), "[]keys and []Keys() should not be equal, they are copies, one item was removed")
  30. }
  31. func TestContains(t *testing.T) {
  32. cmap := NewCMap()
  33. cmap.Set("key1", "value1")
  34. // Test for known values
  35. assert.True(t, cmap.Has("key1"))
  36. assert.Equal(t, "value1", cmap.Get("key1"))
  37. // Test for unknown values
  38. assert.False(t, cmap.Has("key2"))
  39. assert.Nil(t, cmap.Get("key2"))
  40. }
  41. func BenchmarkCMapHas(b *testing.B) {
  42. m := NewCMap()
  43. for i := 0; i < 1000; i++ {
  44. m.Set(string(i), i)
  45. }
  46. b.ResetTimer()
  47. for i := 0; i < b.N; i++ {
  48. m.Has(string(i))
  49. }
  50. }