xref: /aosp_15_r20/art/test/712-varhandle-invocations/src/VarHandleUnitTestCollector.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker import java.io.PrintStream;
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker // Results collector for VarHandle Unit tests
20*795d594fSAndroid Build Coastguard Worker public final class VarHandleUnitTestCollector {
21*795d594fSAndroid Build Coastguard Worker     private final PrintStream out = System.out;
22*795d594fSAndroid Build Coastguard Worker     private final boolean verbose = false;
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker     private int numberOfSuccesses;
25*795d594fSAndroid Build Coastguard Worker     private int numberOfSkips;
26*795d594fSAndroid Build Coastguard Worker     private int numberOfFailures;
27*795d594fSAndroid Build Coastguard Worker     private int consecutiveResults = 0;
28*795d594fSAndroid Build Coastguard Worker     private String current;
29*795d594fSAndroid Build Coastguard Worker     private long startMillis;
30*795d594fSAndroid Build Coastguard Worker 
start(String testName)31*795d594fSAndroid Build Coastguard Worker     public void start(String testName) {
32*795d594fSAndroid Build Coastguard Worker         out.append(testName)
33*795d594fSAndroid Build Coastguard Worker                 .append("...");
34*795d594fSAndroid Build Coastguard Worker         consecutiveResults = 0;
35*795d594fSAndroid Build Coastguard Worker         current = testName;
36*795d594fSAndroid Build Coastguard Worker         startMillis = System.currentTimeMillis();
37*795d594fSAndroid Build Coastguard Worker     }
38*795d594fSAndroid Build Coastguard Worker 
printStatus(String status)39*795d594fSAndroid Build Coastguard Worker     private void printStatus(String status) {
40*795d594fSAndroid Build Coastguard Worker         out.print(status);
41*795d594fSAndroid Build Coastguard Worker         if (verbose) {
42*795d594fSAndroid Build Coastguard Worker             out.print('[');
43*795d594fSAndroid Build Coastguard Worker             out.print(System.currentTimeMillis() - startMillis);
44*795d594fSAndroid Build Coastguard Worker             out.print(']');
45*795d594fSAndroid Build Coastguard Worker         }
46*795d594fSAndroid Build Coastguard Worker         out.println();
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker 
skip()49*795d594fSAndroid Build Coastguard Worker     public void skip() {
50*795d594fSAndroid Build Coastguard Worker         numberOfSkips += 1;
51*795d594fSAndroid Build Coastguard Worker         printStatus("SKIP");
52*795d594fSAndroid Build Coastguard Worker         consecutiveResults++;
53*795d594fSAndroid Build Coastguard Worker     }
54*795d594fSAndroid Build Coastguard Worker 
success()55*795d594fSAndroid Build Coastguard Worker     public void success() {
56*795d594fSAndroid Build Coastguard Worker         numberOfSuccesses += 1;
57*795d594fSAndroid Build Coastguard Worker         printStatus("OK");
58*795d594fSAndroid Build Coastguard Worker         if (consecutiveResults++ > 1) {
59*795d594fSAndroid Build Coastguard Worker             throw new AssertionError("Oops: " + consecutiveResults);
60*795d594fSAndroid Build Coastguard Worker         }
61*795d594fSAndroid Build Coastguard Worker     }
62*795d594fSAndroid Build Coastguard Worker 
fail(String errorMessage)63*795d594fSAndroid Build Coastguard Worker     public void fail(String errorMessage) {
64*795d594fSAndroid Build Coastguard Worker         numberOfFailures += 1;
65*795d594fSAndroid Build Coastguard Worker         printStatus("FAIL");
66*795d594fSAndroid Build Coastguard Worker         out.print(errorMessage);
67*795d594fSAndroid Build Coastguard Worker         consecutiveResults++;
68*795d594fSAndroid Build Coastguard Worker     }
69*795d594fSAndroid Build Coastguard Worker 
printSummary()70*795d594fSAndroid Build Coastguard Worker     public void printSummary() {
71*795d594fSAndroid Build Coastguard Worker         out.append(Integer.toString(numberOfSuccesses))
72*795d594fSAndroid Build Coastguard Worker                 .append(" successes, ")
73*795d594fSAndroid Build Coastguard Worker                 .append(Integer.toString(numberOfSkips))
74*795d594fSAndroid Build Coastguard Worker                 .append(" skips, ")
75*795d594fSAndroid Build Coastguard Worker                 .append(Integer.toString(numberOfFailures))
76*795d594fSAndroid Build Coastguard Worker                 .append(" failures.");
77*795d594fSAndroid Build Coastguard Worker         out.println();
78*795d594fSAndroid Build Coastguard Worker     }
79*795d594fSAndroid Build Coastguard Worker 
failuresOccurred()80*795d594fSAndroid Build Coastguard Worker     boolean failuresOccurred() {
81*795d594fSAndroid Build Coastguard Worker         return numberOfFailures != 0;
82*795d594fSAndroid Build Coastguard Worker     }
83*795d594fSAndroid Build Coastguard Worker }
84