您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

168 行
3.5 KiB

  1. // Copyright 2018 Google Inc. All Rights Reserved.
  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. package profile
  15. import (
  16. "testing"
  17. )
  18. func TestMapMapping(t *testing.T) {
  19. pm := &profileMerger{
  20. p: &Profile{},
  21. mappings: make(map[mappingKey]*Mapping),
  22. mappingsByID: make(map[uint64]mapInfo),
  23. }
  24. for _, tc := range []struct {
  25. desc string
  26. m1 Mapping
  27. m2 Mapping
  28. wantMerged bool
  29. }{
  30. {
  31. desc: "same file name",
  32. m1: Mapping{
  33. ID: 1,
  34. File: "test-file-1",
  35. },
  36. m2: Mapping{
  37. ID: 2,
  38. File: "test-file-1",
  39. },
  40. wantMerged: true,
  41. },
  42. {
  43. desc: "same build ID",
  44. m1: Mapping{
  45. ID: 3,
  46. BuildID: "test-build-id-1",
  47. },
  48. m2: Mapping{
  49. ID: 4,
  50. BuildID: "test-build-id-1",
  51. },
  52. wantMerged: true,
  53. },
  54. {
  55. desc: "same fake mapping",
  56. m1: Mapping{
  57. ID: 5,
  58. },
  59. m2: Mapping{
  60. ID: 6,
  61. },
  62. wantMerged: true,
  63. },
  64. {
  65. desc: "different start",
  66. m1: Mapping{
  67. ID: 7,
  68. Start: 0x1000,
  69. Limit: 0x2000,
  70. BuildID: "test-build-id-2",
  71. },
  72. m2: Mapping{
  73. ID: 8,
  74. Start: 0x3000,
  75. Limit: 0x4000,
  76. BuildID: "test-build-id-2",
  77. },
  78. wantMerged: true,
  79. },
  80. {
  81. desc: "different file name",
  82. m1: Mapping{
  83. ID: 9,
  84. File: "test-file-2",
  85. },
  86. m2: Mapping{
  87. ID: 10,
  88. File: "test-file-3",
  89. },
  90. },
  91. {
  92. desc: "different build id",
  93. m1: Mapping{
  94. ID: 11,
  95. BuildID: "test-build-id-3",
  96. },
  97. m2: Mapping{
  98. ID: 12,
  99. BuildID: "test-build-id-4",
  100. },
  101. },
  102. {
  103. desc: "different size",
  104. m1: Mapping{
  105. ID: 13,
  106. Start: 0x1000,
  107. Limit: 0x3000,
  108. BuildID: "test-build-id-5",
  109. },
  110. m2: Mapping{
  111. ID: 14,
  112. Start: 0x1000,
  113. Limit: 0x5000,
  114. BuildID: "test-build-id-5",
  115. },
  116. },
  117. {
  118. desc: "different offset",
  119. m1: Mapping{
  120. ID: 15,
  121. Offset: 1,
  122. BuildID: "test-build-id-6",
  123. },
  124. m2: Mapping{
  125. ID: 16,
  126. Offset: 2,
  127. BuildID: "test-build-id-6",
  128. },
  129. },
  130. } {
  131. t.Run(tc.desc, func(t *testing.T) {
  132. info1 := pm.mapMapping(&tc.m1)
  133. info2 := pm.mapMapping(&tc.m2)
  134. gotM1, gotM2 := *info1.m, *info2.m
  135. wantM1 := tc.m1
  136. wantM1.ID = gotM1.ID
  137. if gotM1 != wantM1 {
  138. t.Errorf("first mapping got %v, want %v", gotM1, wantM1)
  139. }
  140. if tc.wantMerged {
  141. if gotM1 != gotM2 {
  142. t.Errorf("first mapping got %v, second mapping got %v, want equal", gotM1, gotM2)
  143. }
  144. if info1.offset != 0 {
  145. t.Errorf("first mapping info got offset %d, want 0", info1.offset)
  146. }
  147. if wantOffset := int64(tc.m1.Start) - int64(tc.m2.Start); wantOffset != info2.offset {
  148. t.Errorf("second mapping info got offset %d, want %d", info2.offset, wantOffset)
  149. }
  150. } else {
  151. if gotM1.ID == gotM2.ID {
  152. t.Errorf("first mapping got %v, second mapping got %v, want different IDs", gotM1, gotM2)
  153. }
  154. wantM2 := tc.m2
  155. wantM2.ID = gotM2.ID
  156. if gotM2 != wantM2 {
  157. t.Errorf("second mapping got %v, want %v", gotM2, wantM2)
  158. }
  159. }
  160. })
  161. }
  162. }