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.
 
 
 

48 lines
1.5 KiB

  1. // Copyright 2017 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 measurement
  15. import (
  16. "testing"
  17. )
  18. func TestScale(t *testing.T) {
  19. for _, tc := range []struct {
  20. value int64
  21. fromUnit, toUnit string
  22. wantValue float64
  23. wantUnit string
  24. }{
  25. {1, "s", "ms", 1000, "ms"},
  26. {1, "kb", "b", 1024, "B"},
  27. {1, "kbyte", "b", 1024, "B"},
  28. {1, "kilobyte", "b", 1024, "B"},
  29. {1, "mb", "kb", 1024, "kB"},
  30. {1, "gb", "mb", 1024, "MB"},
  31. {1024, "gb", "tb", 1, "TB"},
  32. {1024, "tb", "pb", 1, "PB"},
  33. {2048, "mb", "auto", 2, "GB"},
  34. {3.1536e7, "s", "auto", 1, "yrs"},
  35. {-1, "s", "ms", -1000, "ms"},
  36. {1, "foo", "count", 1, ""},
  37. {1, "foo", "bar", 1, "bar"},
  38. } {
  39. if gotValue, gotUnit := Scale(tc.value, tc.fromUnit, tc.toUnit); gotValue != tc.wantValue || gotUnit != tc.wantUnit {
  40. t.Errorf("Scale(%d, %q, %q) = (%f, %q), want (%f, %q)",
  41. tc.value, tc.fromUnit, tc.toUnit, gotValue, gotUnit, tc.wantValue, tc.wantUnit)
  42. }
  43. }
  44. }