|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package pprofile |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestLocationEqual(t *testing.T) { |
| 13 | + for _, tt := range []struct { |
| 14 | + name string |
| 15 | + orig Location |
| 16 | + dest Location |
| 17 | + want bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "empty locations", |
| 21 | + orig: NewLocation(), |
| 22 | + dest: NewLocation(), |
| 23 | + want: true, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "non-empty locations", |
| 27 | + orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 28 | + dest: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 29 | + want: true, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "with non-equal mapping index", |
| 33 | + orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 34 | + dest: buildLocation(2, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 35 | + want: false, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "with non-equal address", |
| 39 | + orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 40 | + dest: buildLocation(1, 3, []int32{3}, true, buildLine(1, 2, 3)), |
| 41 | + want: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "with non-equal attribute indices", |
| 45 | + orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 46 | + dest: buildLocation(1, 2, []int32{5}, true, buildLine(1, 2, 3)), |
| 47 | + want: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "with non-equal is folded", |
| 51 | + orig: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 52 | + dest: buildLocation(1, 2, []int32{3}, false, buildLine(1, 2, 3)), |
| 53 | + want: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "with non-equal lines", |
| 57 | + orig: buildLocation(1, 2, []int32{3}, true, buildLine(4, 5, 6)), |
| 58 | + dest: buildLocation(1, 2, []int32{3}, true, buildLine(1, 2, 3)), |
| 59 | + want: false, |
| 60 | + }, |
| 61 | + } { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + if tt.want { |
| 64 | + assert.True(t, tt.orig.Equal(tt.dest)) |
| 65 | + } else { |
| 66 | + assert.False(t, tt.orig.Equal(tt.dest)) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func buildLocation(mapIdx int32, addr uint64, attrIdxs []int32, isFolded bool, line Line) Location { |
| 73 | + l := NewLocation() |
| 74 | + l.SetMappingIndex(mapIdx) |
| 75 | + l.SetAddress(addr) |
| 76 | + l.AttributeIndices().FromRaw(attrIdxs) |
| 77 | + l.SetIsFolded(isFolded) |
| 78 | + line.MoveTo(l.Line().AppendEmpty()) |
| 79 | + return l |
| 80 | +} |
0 commit comments