|
1 | | -// Copyright 2024 The Hugo Authors. All rights reserved. |
| 1 | +// Copyright 2025 The Hugo Authors. All rights reserved. |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
|
14 | 14 | package hstrings |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "reflect" |
17 | 18 | "regexp" |
18 | 19 | "testing" |
19 | 20 |
|
@@ -43,6 +44,84 @@ func TestGetOrCompileRegexp(t *testing.T) { |
43 | 44 | c.Assert(re.MatchString("123"), qt.Equals, true) |
44 | 45 | } |
45 | 46 |
|
| 47 | +func TestUniqueStrings(t *testing.T) { |
| 48 | + in := []string{"a", "b", "a", "b", "c", "", "a", "", "d"} |
| 49 | + output := UniqueStrings(in) |
| 50 | + expected := []string{"a", "b", "c", "", "d"} |
| 51 | + if !reflect.DeepEqual(output, expected) { |
| 52 | + t.Errorf("Expected %#v, got %#v\n", expected, output) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestUniqueStringsReuse(t *testing.T) { |
| 57 | + in := []string{"a", "b", "a", "b", "c", "", "a", "", "d"} |
| 58 | + output := UniqueStringsReuse(in) |
| 59 | + expected := []string{"a", "b", "c", "", "d"} |
| 60 | + if !reflect.DeepEqual(output, expected) { |
| 61 | + t.Errorf("Expected %#v, got %#v\n", expected, output) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestUniqueStringsSorted(t *testing.T) { |
| 66 | + c := qt.New(t) |
| 67 | + in := []string{"a", "a", "b", "c", "b", "", "a", "", "d"} |
| 68 | + output := UniqueStringsSorted(in) |
| 69 | + expected := []string{"", "a", "b", "c", "d"} |
| 70 | + c.Assert(output, qt.DeepEquals, expected) |
| 71 | + c.Assert(UniqueStringsSorted(nil), qt.IsNil) |
| 72 | +} |
| 73 | + |
| 74 | +func BenchmarkUniqueStrings(b *testing.B) { |
| 75 | + input := []string{"a", "b", "d", "e", "d", "h", "a", "i"} |
| 76 | + |
| 77 | + b.Run("Safe", func(b *testing.B) { |
| 78 | + for i := 0; i < b.N; i++ { |
| 79 | + result := UniqueStrings(input) |
| 80 | + if len(result) != 6 { |
| 81 | + b.Fatalf("invalid count: %d", len(result)) |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + b.Run("Reuse slice", func(b *testing.B) { |
| 87 | + b.StopTimer() |
| 88 | + inputs := make([][]string, b.N) |
| 89 | + for i := 0; i < b.N; i++ { |
| 90 | + inputc := make([]string, len(input)) |
| 91 | + copy(inputc, input) |
| 92 | + inputs[i] = inputc |
| 93 | + } |
| 94 | + b.StartTimer() |
| 95 | + for i := 0; i < b.N; i++ { |
| 96 | + inputc := inputs[i] |
| 97 | + |
| 98 | + result := UniqueStringsReuse(inputc) |
| 99 | + if len(result) != 6 { |
| 100 | + b.Fatalf("invalid count: %d", len(result)) |
| 101 | + } |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + b.Run("Reuse slice sorted", func(b *testing.B) { |
| 106 | + b.StopTimer() |
| 107 | + inputs := make([][]string, b.N) |
| 108 | + for i := 0; i < b.N; i++ { |
| 109 | + inputc := make([]string, len(input)) |
| 110 | + copy(inputc, input) |
| 111 | + inputs[i] = inputc |
| 112 | + } |
| 113 | + b.StartTimer() |
| 114 | + for i := 0; i < b.N; i++ { |
| 115 | + inputc := inputs[i] |
| 116 | + |
| 117 | + result := UniqueStringsSorted(inputc) |
| 118 | + if len(result) != 6 { |
| 119 | + b.Fatalf("invalid count: %d", len(result)) |
| 120 | + } |
| 121 | + } |
| 122 | + }) |
| 123 | +} |
| 124 | + |
46 | 125 | func BenchmarkGetOrCompileRegexp(b *testing.B) { |
47 | 126 | for i := 0; i < b.N; i++ { |
48 | 127 | GetOrCompileRegexp(`\d+`) |
|
0 commit comments