| 1 |
// Copyright (c) Bartłomiej Płotka @bwplotka |
| 2 |
// Licensed under the Apache License 2.0. |
| 3 |
|
| 4 |
// Copyright 2024 Google LLC |
| 5 |
// |
| 6 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
// you may not use this file except in compliance with the License. |
| 8 |
// You may obtain a copy of the License at |
| 9 |
// |
| 10 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
// |
| 12 |
// Unless required by applicable law or agreed to in writing, software |
| 13 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
// See the License for the specific language governing permissions and |
| 16 |
// limitations under the License. |
| 17 |
|
| 18 |
// Copyright 2024 Prometheus Team |
| 19 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 |
// you may not use this file except in compliance with the License. |
| 21 |
// You may obtain a copy of the License at |
| 22 |
// |
| 23 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 24 |
// |
| 25 |
// Unless required by applicable law or agreed to in writing, software |
| 26 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 27 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 |
// See the License for the specific language governing permissions and |
| 29 |
// limitations under the License. |
| 30 |
|
| 31 |
package writev2 |
| 32 |
|
| 33 |
import ( |
| 34 |
"testing" |
| 35 |
|
| 36 |
"github.com/google/go-cmp/cmp" |
| 37 |
) |
| 38 |
|
| 39 |
func requireEqual(t testing.TB, expected, got any) { |
| 40 |
if diff := cmp.Diff(expected, got); diff != "" { |
| 41 |
t.Fatal(diff) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestSymbolsTable(t *testing.T) { |
| 46 |
s := NewSymbolTable() |
| 47 |
requireEqual(t, []string{""}, s.Symbols()) |
| 48 |
requireEqual(t, uint32(0), s.Symbolize("")) |
| 49 |
requireEqual(t, []string{""}, s.Symbols()) |
| 50 |
|
| 51 |
requireEqual(t, uint32(1), s.Symbolize("abc")) |
| 52 |
requireEqual(t, []string{"", "abc"}, s.Symbols()) |
| 53 |
|
| 54 |
requireEqual(t, uint32(2), s.Symbolize("__name__")) |
| 55 |
requireEqual(t, []string{"", "abc", "__name__"}, s.Symbols()) |
| 56 |
|
| 57 |
requireEqual(t, uint32(3), s.Symbolize("foo")) |
| 58 |
requireEqual(t, []string{"", "abc", "__name__", "foo"}, s.Symbols()) |
| 59 |
|
| 60 |
s.Reset() |
| 61 |
requireEqual(t, []string{""}, s.Symbols()) |
| 62 |
requireEqual(t, uint32(0), s.Symbolize("")) |
| 63 |
|
| 64 |
requireEqual(t, uint32(1), s.Symbolize("__name__")) |
| 65 |
requireEqual(t, []string{"", "__name__"}, s.Symbols()) |
| 66 |
|
| 67 |
requireEqual(t, uint32(2), s.Symbolize("abc")) |
| 68 |
requireEqual(t, []string{"", "__name__", "abc"}, s.Symbols()) |
| 69 |
|
| 70 |
ls := []string{"__name__", "qwer", "zxcv", "1234"} |
| 71 |
encoded := s.SymbolizeLabels(ls, nil) |
| 72 |
requireEqual(t, []uint32{1, 3, 4, 5}, encoded) |
| 73 |
decoded := DesymbolizeLabels(encoded, s.Symbols(), nil) |
| 74 |
requireEqual(t, ls, decoded) |
| 75 |
|
| 76 |
// Different buf. |
| 77 |
ls = []string{"__name__", "qwer", "zxcv2222", "1234"} |
| 78 |
encoded = s.SymbolizeLabels(ls, []uint32{1, 3, 4, 5}) |
| 79 |
requireEqual(t, []uint32{1, 3, 6, 5}, encoded) |
| 80 |
} |