|
| 1 | +// Copyright 2023, Ryan Parman |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package corefunc |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/northwood-labs/terraform-provider-corefunc/corefunc/types" |
| 22 | + "github.com/northwood-labs/terraform-provider-corefunc/testfixtures" |
| 23 | +) |
| 24 | + |
| 25 | +func ExampleStrIterativeReplace() { |
| 26 | + output := StrIterativeReplace( |
| 27 | + "This is a string for testing replacements. New Relic. Set-up.", |
| 28 | + []types.Replacement{ |
| 29 | + {Old: ".", New: ""}, |
| 30 | + {Old: " ", New: "_"}, |
| 31 | + {Old: "-", New: "_"}, |
| 32 | + {Old: "New_Relic", New: "datadog"}, |
| 33 | + {Old: "This", New: "this"}, |
| 34 | + {Old: "Set_up", New: "setup"}, |
| 35 | + }, |
| 36 | + ) |
| 37 | + |
| 38 | + fmt.Println(output) |
| 39 | + |
| 40 | + // Output: |
| 41 | + // this_is_a_string_for_testing_replacements_datadog_setup |
| 42 | +} |
| 43 | + |
| 44 | +func TestStrIterativeReplace(t *testing.T) { |
| 45 | + for name, tc := range testfixtures.StrIterativeReplaceTestTable { |
| 46 | + t.Run(name, func(t *testing.T) { |
| 47 | + output := StrIterativeReplace(tc.Input, tc.Replacements) |
| 48 | + |
| 49 | + if output != tc.Expected { |
| 50 | + t.Errorf("Expected %s, got %s", tc.Expected, output) |
| 51 | + } |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func BenchmarkStrIterativeReplace(b *testing.B) { |
| 57 | + b.ReportAllocs() |
| 58 | + for name, tc := range testfixtures.StrIterativeReplaceTestTable { |
| 59 | + b.Run(name, func(b *testing.B) { |
| 60 | + b.ResetTimer() |
| 61 | + for i := 0; i < b.N; i++ { |
| 62 | + _ = StrIterativeReplace(tc.Input, tc.Replacements) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func BenchmarkStrIterativeReplaceParallel(b *testing.B) { |
| 69 | + b.ReportAllocs() |
| 70 | + for name, tc := range testfixtures.StrIterativeReplaceTestTable { |
| 71 | + b.Run(name, func(b *testing.B) { |
| 72 | + b.ResetTimer() |
| 73 | + b.RunParallel(func(pb *testing.PB) { |
| 74 | + for pb.Next() { |
| 75 | + _ = StrIterativeReplace(tc.Input, tc.Replacements) |
| 76 | + } |
| 77 | + }) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func FuzzStrIterativeReplace(f *testing.F) { |
| 83 | + for _, tc := range testfixtures.StrIterativeReplaceTestTable { |
| 84 | + f.Add(tc.Input) |
| 85 | + f.Add(tc.Expected) |
| 86 | + |
| 87 | + for _, r := range tc.Replacements { |
| 88 | + f.Add(r.Old) |
| 89 | + f.Add(r.New) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + f.Fuzz( |
| 94 | + func(t *testing.T, in string) { |
| 95 | + _ = StrIterativeReplace(in, []types.Replacement{ |
| 96 | + {Old: in, New: in}, |
| 97 | + }) |
| 98 | + }, |
| 99 | + ) |
| 100 | +} |
0 commit comments