You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

115 lines
3.2 KiB

  1. // Copyright 2016 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 TestSampleIndexByName(t *testing.T) {
  19. for _, c := range []struct {
  20. desc string
  21. sampleTypes []string
  22. defaultSampleType string
  23. index string
  24. want int
  25. wantError bool
  26. }{
  27. {
  28. desc: "use last by default",
  29. index: "",
  30. want: 1,
  31. sampleTypes: []string{"zero", "default"},
  32. },
  33. {
  34. desc: "honour specified default",
  35. index: "",
  36. want: 1,
  37. defaultSampleType: "default",
  38. sampleTypes: []string{"zero", "default", "two"},
  39. },
  40. {
  41. desc: "invalid default is ignored",
  42. index: "",
  43. want: 2,
  44. defaultSampleType: "non-existent",
  45. sampleTypes: []string{"zero", "one", "default"},
  46. },
  47. {
  48. desc: "index by int",
  49. index: "0",
  50. want: 0,
  51. sampleTypes: []string{"zero", "one", "two"},
  52. },
  53. {
  54. desc: "index by int ignores default",
  55. index: "0",
  56. want: 0,
  57. defaultSampleType: "default",
  58. sampleTypes: []string{"zero", "default", "two"},
  59. },
  60. {
  61. desc: "index by name",
  62. index: "two",
  63. want: 2,
  64. sampleTypes: []string{"zero", "one", "two", "three"},
  65. },
  66. {
  67. desc: "index by name ignores default",
  68. index: "zero",
  69. want: 0,
  70. defaultSampleType: "default",
  71. sampleTypes: []string{"zero", "default", "two"},
  72. },
  73. {
  74. desc: "out of bound int causes error",
  75. index: "100",
  76. wantError: true,
  77. sampleTypes: []string{"zero", "default"},
  78. },
  79. {
  80. desc: "unknown name causes error",
  81. index: "does not exist",
  82. wantError: true,
  83. sampleTypes: []string{"zero", "default"},
  84. },
  85. {
  86. desc: "'inused_{x}' recognized for legacy '{x}'",
  87. index: "inuse_zero",
  88. want: 0,
  89. sampleTypes: []string{"zero", "default"},
  90. },
  91. } {
  92. p := &Profile{
  93. DefaultSampleType: c.defaultSampleType,
  94. SampleType: []*ValueType{},
  95. }
  96. for _, st := range c.sampleTypes {
  97. p.SampleType = append(p.SampleType, &ValueType{Type: st, Unit: "milliseconds"})
  98. }
  99. got, err := p.SampleIndexByName(c.index)
  100. switch {
  101. case c.wantError && err == nil:
  102. t.Errorf("%s: error should have been returned not index=%d, err=%v", c.desc, got, err)
  103. case !c.wantError && err != nil:
  104. t.Errorf("%s: unexpected got index=%d, err=%v; wanted index=%d, err=nil", c.desc, got, err, c.want)
  105. case !c.wantError && got != c.want:
  106. t.Errorf("%s: got index=%d, want index=%d", c.desc, got, c.want)
  107. }
  108. }
  109. }