histogram_test.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2019 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "tuningfork/histogram.h"
  17. #include "gtest/gtest.h"
  18. namespace histogram_test {
  19. using namespace tuningfork;
  20. const char kEmptyHistogramJson[] = "{\"pmax\":[],\"cnts\":[]}";
  21. const char kEmpty0To10Json[] =
  22. "{\"pmax\":[0.00,1.00,2.00,3.00,4.00,5.00,6.00,7.00,8.00,9.00,10.00,99999],"
  23. "\"cnts\":[0,0,0,0,0,0,0,0,0,0,0,0]}";
  24. const char kAdd1AutoJson[] =
  25. "{\"pmax\":[0.60,0.70,0.80,0.90,1.00,1.10,1.20,1.30,1.40,99999],"
  26. "\"cnts\":[0,0,0,0,0,1,0,0,0,0]}";
  27. const char kAdd10To10Json[] =
  28. "{\"pmax\":[0.00,1.00,2.00,3.00,4.00,5.00,6.00,7.00,8.00,9.00,10.00,99999],"
  29. "\"cnts\":[0,0,1,0,0,0,0,0,0,0,0,0]}";
  30. TEST(HistogramTest, DefaultEmpty) {
  31. Histogram h;
  32. EXPECT_EQ(h.Count(), 0) << "Initialized Histogram not empty";
  33. EXPECT_EQ(h.ToJSON(), kEmptyHistogramJson) << "Empty histogram bad";
  34. }
  35. TEST(HistogramTest, Empty0To10) {
  36. Histogram h(0, 10, 10);
  37. EXPECT_EQ(h.Count(), 0) << "Initialized Histogram not empty";
  38. EXPECT_EQ(h.ToJSON(), kEmpty0To10Json) << "Empty 0-10 histogram bad";
  39. }
  40. TEST(HistogramTest, AddOneToAutoSizing) {
  41. Histogram h(0, 0, 8);
  42. EXPECT_EQ(h.Count(), 0) << "Initialized auto-sizing Histogram not empty";
  43. h.Add(1.0);
  44. EXPECT_EQ(h.Count(), 0) << "Add 0 should not be counted";
  45. EXPECT_EQ(h.ToJSON(), kEmptyHistogramJson) << "Empty histogram bad";
  46. h.CalcBucketsFromSamples();
  47. EXPECT_EQ(h.Count(), 1) << "Add 1 was not counted";
  48. EXPECT_EQ(h.ToJSON(), kAdd1AutoJson) << "Add 1 auto-sizing bad";
  49. }
  50. TEST(HistogramTest, AddOneTo0To10) {
  51. Histogram h(0, 10, 10);
  52. EXPECT_EQ(h.Count(), 0) << "Initialized Histogram not empty";
  53. h.Add(1.0);
  54. EXPECT_EQ(h.Count(), 1) << "Add 1 was not counted";
  55. EXPECT_EQ(h.ToJSON(), kAdd10To10Json) << "Add 1 0-10 histogram bad";
  56. h.Clear();
  57. EXPECT_EQ(h.ToJSON(), kEmpty0To10Json) << "Clear 0-10 histogram bad";
  58. }
  59. } // namespace histogram_test