Skip to content

Commit

Permalink
Idiomatic bitwise operations
Browse files Browse the repository at this point in the history
  • Loading branch information
diist authored and arp242 committed Oct 18, 2023
1 parent 769aaa7 commit 2f2332a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fsnotify.go
Expand Up @@ -93,7 +93,7 @@ func (o Op) String() string {
}

// Has reports if this operation has the given operation.
func (o Op) Has(h Op) bool { return o&h == h }
func (o Op) Has(h Op) bool { return o&h != 0 }

// Has reports if this event has the given operation.
func (e Event) Has(op Op) bool { return e.Op.Has(op) }
Expand Down
41 changes: 41 additions & 0 deletions fsnotify_test.go
Expand Up @@ -1549,6 +1549,47 @@ func TestWatchList(t *testing.T) {
}
}

func TestOpHas(t *testing.T) {
tests := []struct {
name string
o Op
h Op
want bool
}{
{
name: "single bit match",
o: Remove,
h: Remove,
want: true,
},
{
name: "single bit no match",
o: Remove,
h: Create,
want: false,
},
{
name: "two bits match",
o: Remove | Create,
h: Create,
want: true,
},
{
name: "two bits no match",
o: Remove | Create,
h: Chmod,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.o.Has(tt.h); got != tt.want {
t.Errorf("Has() = %v, want %v", got, tt.want)
}
})
}
}

func BenchmarkWatch(b *testing.B) {
w, err := NewWatcher()
if err != nil {
Expand Down

0 comments on commit 2f2332a

Please sign in to comment.