tailscale/util/slicesx/slicesx_test.go
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

238 lines
6.1 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package slicesx
import (
"reflect"
"slices"
"testing"
qt "github.com/frankban/quicktest"
)
func TestInterleave(t *testing.T) {
testCases := []struct {
name string
a, b []int
want []int
}{
{name: "equal", a: []int{1, 3, 5}, b: []int{2, 4, 6}, want: []int{1, 2, 3, 4, 5, 6}},
{name: "short_b", a: []int{1, 3, 5}, b: []int{2, 4}, want: []int{1, 2, 3, 4, 5}},
{name: "short_a", a: []int{1, 3}, b: []int{2, 4, 6}, want: []int{1, 2, 3, 4, 6}},
{name: "len_1", a: []int{1}, b: []int{2, 4, 6}, want: []int{1, 2, 4, 6}},
{name: "nil_a", a: nil, b: []int{2, 4, 6}, want: []int{2, 4, 6}},
{name: "nil_all", a: nil, b: nil, want: nil},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
merged := Interleave(tc.a, tc.b)
if !reflect.DeepEqual(merged, tc.want) {
t.Errorf("got %v; want %v", merged, tc.want)
}
})
}
}
func BenchmarkInterleave(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
Interleave(
[]int{1, 2, 3},
[]int{9, 8, 7},
)
}
}
func TestShuffle(t *testing.T) {
var sl []int
for i := range 100 {
sl = append(sl, i)
}
var wasShuffled bool
for try := 0; try < 10; try++ {
shuffled := slices.Clone(sl)
Shuffle(shuffled)
if !reflect.DeepEqual(shuffled, sl) {
wasShuffled = true
break
}
}
if !wasShuffled {
t.Errorf("expected shuffle after 10 tries")
}
}
func TestPartition(t *testing.T) {
var sl []int
for i := 1; i <= 10; i++ {
sl = append(sl, i)
}
evens, odds := Partition(sl, func(elem int) bool {
return elem%2 == 0
})
wantEvens := []int{2, 4, 6, 8, 10}
wantOdds := []int{1, 3, 5, 7, 9}
if !reflect.DeepEqual(evens, wantEvens) {
t.Errorf("evens: got %v, want %v", evens, wantEvens)
}
if !reflect.DeepEqual(odds, wantOdds) {
t.Errorf("odds: got %v, want %v", odds, wantOdds)
}
}
func TestEqualSameNil(t *testing.T) {
c := qt.New(t)
c.Check(EqualSameNil([]string{"a"}, []string{"a"}), qt.Equals, true)
c.Check(EqualSameNil([]string{"a"}, []string{"b"}), qt.Equals, false)
c.Check(EqualSameNil([]string{"a"}, []string{}), qt.Equals, false)
c.Check(EqualSameNil([]string{}, []string{}), qt.Equals, true)
c.Check(EqualSameNil(nil, []string{}), qt.Equals, false)
c.Check(EqualSameNil([]string{}, nil), qt.Equals, false)
c.Check(EqualSameNil[[]string](nil, nil), qt.Equals, true)
}
func TestFilter(t *testing.T) {
var sl []int
for i := 1; i <= 10; i++ {
sl = append(sl, i)
}
evens := Filter(nil, sl, func(elem int) bool {
return elem%2 == 0
})
want := []int{2, 4, 6, 8, 10}
if !reflect.DeepEqual(evens, want) {
t.Errorf("evens: got %v, want %v", evens, want)
}
}
func TestFilterNoAllocations(t *testing.T) {
var sl []int
for i := 1; i <= 10; i++ {
sl = append(sl, i)
}
want := []int{2, 4, 6, 8, 10}
allocs := testing.AllocsPerRun(1000, func() {
src := slices.Clone(sl)
evens := Filter(src[:0], src, func(elem int) bool {
return elem%2 == 0
})
if !slices.Equal(evens, want) {
t.Errorf("evens: got %v, want %v", evens, want)
}
})
// 1 alloc for 'src', nothing else
if allocs != 1 {
t.Fatalf("got %.4f allocs, want 1", allocs)
}
}
func TestAppendNonzero(t *testing.T) {
v := []string{"one", "two", "", "four"}
got := AppendNonzero(nil, v)
want := []string{"one", "two", "four"}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v; want %v", got, want)
}
got = AppendNonzero(v[:0], v)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v; want %v", got, want)
}
}
func TestAppendMatching(t *testing.T) {
v := []string{"one", "two", "three", "four"}
got := AppendMatching(v[:0], v, func(s string) bool { return len(s) > 3 })
want := []string{"three", "four"}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v; want %v", got, want)
}
wantOrigMem := []string{"three", "four", "three", "four"}
if !reflect.DeepEqual(v, wantOrigMem) {
t.Errorf("got %v; want %v", v, wantOrigMem)
}
}
func TestCutPrefix(t *testing.T) {
tests := []struct {
name string
s, prefix []int
after []int
found bool
}{
{"has-prefix", []int{1, 2, 3}, []int{1}, []int{2, 3}, true},
{"exact-prefix", []int{1, 2, 3}, []int{1, 2, 3}, []int{}, true},
{"blank-prefix", []int{1, 2, 3}, []int{}, []int{1, 2, 3}, true},
{"no-prefix", []int{1, 2, 3}, []int{42}, []int{1, 2, 3}, false},
{"blank-slice", []int{}, []int{42}, []int{}, false},
{"blank-all", []int{}, []int{}, []int{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if after, found := CutPrefix(tt.s, tt.prefix); !slices.Equal(after, tt.after) || found != tt.found {
t.Errorf("CutPrefix(%v, %v) = %v, %v; want %v, %v", tt.s, tt.prefix, after, found, tt.after, tt.found)
}
})
}
}
func TestCutSuffix(t *testing.T) {
tests := []struct {
name string
s, suffix []int
before []int
found bool
}{
{"has-suffix", []int{1, 2, 3}, []int{3}, []int{1, 2}, true},
{"exact-suffix", []int{1, 2, 3}, []int{1, 2, 3}, []int{}, true},
{"blank-suffix", []int{1, 2, 3}, []int{}, []int{1, 2, 3}, true},
{"no-suffix", []int{1, 2, 3}, []int{42}, []int{1, 2, 3}, false},
{"blank-slice", []int{}, []int{42}, []int{}, false},
{"blank-all", []int{}, []int{}, []int{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if before, found := CutSuffix(tt.s, tt.suffix); !slices.Equal(before, tt.before) || found != tt.found {
t.Errorf("CutSuffix(%v, %v) = %v, %v; want %v, %v", tt.s, tt.suffix, before, found, tt.before, tt.found)
}
})
}
}
func TestFirstLastEqual(t *testing.T) {
tests := []struct {
name string
in string
v byte
f func([]byte, byte) bool
want bool
}{
{"first-empty", "", 'f', FirstEqual[byte], false},
{"first-true", "foo", 'f', FirstEqual[byte], true},
{"first-false", "foo", 'b', FirstEqual[byte], false},
{"last-empty", "", 'f', LastEqual[byte], false},
{"last-true", "bar", 'r', LastEqual[byte], true},
{"last-false", "bar", 'o', LastEqual[byte], false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.f([]byte(tt.in), tt.v); got != tt.want {
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
}