xref: /llvm-project/llvm/unittests/tools/llvm-exegesis/ProgressMeterTest.cpp (revision 459a82e6890ff41e30d486f36c8c7ec22628bb7a)
1 //===-- ProgressMeterTest.cpp -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ProgressMeter.h"
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
12 
13 namespace llvm {
14 namespace exegesis {
15 
16 namespace {
17 
18 struct PreprogrammedClock {
19   using duration = std::chrono::seconds;
20   using rep = duration::rep;
21   using period = duration::period;
22   using time_point = std::chrono::time_point<PreprogrammedClock, duration>;
23 
24   static constexpr bool is_steady = true;
25 
26   static time_point now() noexcept;
27 };
28 
29 static int CurrentTimePoint = 0;
30 auto Pt(int i) {
31   return PreprogrammedClock::time_point(PreprogrammedClock::duration(i));
32 }
33 static const std::array<const PreprogrammedClock::time_point, 10> TimePoints = {
34     Pt(0),  Pt(5),  Pt(6),  Pt(20), Pt(20),
35     Pt(35), Pt(37), Pt(75), Pt(77), Pt(100)};
36 
37 PreprogrammedClock::time_point PreprogrammedClock::now() noexcept {
38   time_point p = TimePoints[CurrentTimePoint];
39   ++CurrentTimePoint;
40   return p;
41 }
42 
43 TEST(ProgressMeterTest, Integration) {
44   CurrentTimePoint = 0;
45   std::string TempString;
46   raw_string_ostream SS(TempString);
47   ProgressMeter<PreprogrammedClock> m(5, SS);
48   for (int i = 0; i != 5; ++i)
49     decltype(m)::ProgressMeterStep s(&m);
50   ASSERT_EQ("Processing...  20%, ETA 00:20\n"
51             "Processing...  40%, ETA 00:29\n"
52             "Processing...  60%, ETA 00:23\n"
53             "Processing...  80%, ETA 00:18\n"
54             "Processing... 100%, ETA 00:00\n",
55             TempString);
56 }
57 
58 } // namespace
59 } // namespace exegesis
60 } // namespace llvm
61