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.

set_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package caps
  4. import "testing"
  5. import "reflect"
  6. func TestSets(t *testing.T) {
  7. s1 := NewSet()
  8. s1.Enable(AccountTag, EchoMessage, UserhostInNames)
  9. if !(s1.Has(AccountTag) && s1.Has(EchoMessage) && s1.Has(UserhostInNames)) {
  10. t.Error("Did not have the tags we expected")
  11. }
  12. if s1.Has(STS) {
  13. t.Error("Has() returned true when we don't have the given capability")
  14. }
  15. s1.Disable(AccountTag)
  16. if s1.Has(AccountTag) {
  17. t.Error("Disable() did not correctly disable the given capability")
  18. }
  19. enabledCaps := NewSet()
  20. enabledCaps.Union(s1)
  21. expectedCaps := NewSet(EchoMessage, UserhostInNames)
  22. if !reflect.DeepEqual(enabledCaps, expectedCaps) {
  23. t.Errorf("Enabled and expected capability lists do not match: %v, %v", enabledCaps, expectedCaps)
  24. }
  25. // make sure re-enabling doesn't add to the count or something weird like that
  26. s1.Enable(EchoMessage)
  27. // make sure add and remove work fine
  28. s1.Add(InviteNotify)
  29. s1.Remove(EchoMessage)
  30. if !s1.Has(InviteNotify) || s1.Has(EchoMessage) {
  31. t.Error("Add/Remove don't work")
  32. }
  33. // test Strings()
  34. values := make(Values)
  35. values[InviteNotify] = "invitemepls"
  36. actualCap301ValuesString := s1.Strings(Cap301, values, 0)
  37. expectedCap301ValuesString := []string{"invite-notify userhost-in-names"}
  38. if !reflect.DeepEqual(actualCap301ValuesString, expectedCap301ValuesString) {
  39. t.Errorf("Generated Cap301 values string [%v] did not match expected values string [%v]", actualCap301ValuesString, expectedCap301ValuesString)
  40. }
  41. actualCap302ValuesString := s1.Strings(Cap302, values, 0)
  42. expectedCap302ValuesString := []string{"invite-notify=invitemepls userhost-in-names"}
  43. if !reflect.DeepEqual(actualCap302ValuesString, expectedCap302ValuesString) {
  44. t.Errorf("Generated Cap302 values string [%s] did not match expected values string [%s]", actualCap302ValuesString, expectedCap302ValuesString)
  45. }
  46. }
  47. func TestSubtract(t *testing.T) {
  48. s1 := NewSet(AccountTag, EchoMessage, UserhostInNames, ServerTime)
  49. toRemove := NewSet(UserhostInNames, EchoMessage)
  50. s1.Subtract(toRemove)
  51. if !reflect.DeepEqual(s1, NewSet(AccountTag, ServerTime)) {
  52. t.Errorf("subtract doesn't work")
  53. }
  54. }
  55. func BenchmarkSetReads(b *testing.B) {
  56. set := NewSet(UserhostInNames, EchoMessage)
  57. b.ResetTimer()
  58. for i := 0; i < b.N; i++ {
  59. set.Has(UserhostInNames)
  60. set.Has(LabeledResponse)
  61. set.Has(EchoMessage)
  62. set.Has(Nope)
  63. }
  64. }
  65. func BenchmarkSetWrites(b *testing.B) {
  66. for i := 0; i < b.N; i++ {
  67. set := NewSet(UserhostInNames, EchoMessage)
  68. set.Add(Nope)
  69. set.Add(ExtendedJoin)
  70. set.Remove(UserhostInNames)
  71. set.Remove(LabeledResponse)
  72. }
  73. }