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.

69 lines
1.4 KiB

  1. package cmap
  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(
  30. t,
  31. len(keys),
  32. len(cmap.Keys()),
  33. "[]keys and []Keys() should not be equal, they are copies, one item was removed",
  34. )
  35. }
  36. func TestContains(t *testing.T) {
  37. cmap := NewCMap()
  38. cmap.Set("key1", "value1")
  39. // Test for known values
  40. assert.True(t, cmap.Has("key1"))
  41. assert.Equal(t, "value1", cmap.Get("key1"))
  42. // Test for unknown values
  43. assert.False(t, cmap.Has("key2"))
  44. assert.Nil(t, cmap.Get("key2"))
  45. }
  46. func BenchmarkCMapHas(b *testing.B) {
  47. m := NewCMap()
  48. for i := 0; i < 1000; i++ {
  49. m.Set(string(i), i)
  50. }
  51. b.ResetTimer()
  52. for i := 0; i < b.N; i++ {
  53. m.Has(string(i))
  54. }
  55. }