Skip to content

Commit

Permalink
Merge branch 'main' of github.com:binwiederhier/ntfy
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed May 13, 2023
2 parents c1db1e4 + d674e02 commit bd81aef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 6 additions & 8 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"golang.org/x/time/rate"
"io"
"math/rand"
"net/netip"
Expand All @@ -17,6 +16,8 @@ import (
"sync"
"time"

"golang.org/x/time/rate"

"github.com/gabriel-vasile/mimetype"
"golang.org/x/term"
)
Expand Down Expand Up @@ -67,15 +68,12 @@ func ContainsIP(haystack []netip.Prefix, needle netip.Addr) bool {

// ContainsAll returns true if all needles are contained in haystack
func ContainsAll[T comparable](haystack []T, needles []T) bool {
matches := 0
for _, s := range haystack {
for _, needle := range needles {
if s == needle {
matches++
}
for _, needle := range needles {
if !Contains(haystack, needle) {
return false
}
}
return matches == len(needles)
return true
}

// SplitNoEmpty splits a string using strings.Split, but filters out empty strings
Expand Down
8 changes: 7 additions & 1 deletion util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package util

import (
"errors"
"golang.org/x/time/rate"
"io"
"net/netip"
"os"
Expand All @@ -11,6 +10,8 @@ import (
"testing"
"time"

"golang.org/x/time/rate"

"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -49,6 +50,11 @@ func TestContains(t *testing.T) {
require.False(t, Contains(s, 3))
}

func TestContainsAll(t *testing.T) {
require.True(t, ContainsAll([]int{1, 2, 3}, []int{2, 3}))
require.False(t, ContainsAll([]int{1, 1}, []int{1, 2}))
}

func TestContainsIP(t *testing.T) {
require.True(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("1.1.1.1")))
require.True(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("fd12:1234:5678::9876")))
Expand Down

0 comments on commit bd81aef

Please sign in to comment.