• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 #include <chre/util/nanoapp/log.h>
18 #include <shared/macros.h>
19 #include <shared/send_message.h>
20 #include <shared/test_success_marker.h>
21 
22 #define LOG_TAG "[SuccessMarkerTest]"
23 
24 namespace nanoapp_testing {
25 
TestSuccessMarker(uint32_t numStages)26 TestSuccessMarker::TestSuccessMarker(uint32_t numStages) {
27   if (numStages > 32) {
28     EXPECT_FAIL_RETURN("Total number of stage should be less than 33, got %d",
29                        &numStages);
30   }
31   mAllFinished = (UINT64_C(1) << numStages) - 1;
32 }
33 
markStage(uint32_t stage)34 void TestSuccessMarker::markStage(uint32_t stage) {
35   uint32_t finishedBit = (1 << stage);
36   if ((mAllFinished & finishedBit) == 0) {
37     EXPECT_FAIL_RETURN("markSuccess invalid stage", &stage);
38   }
39   if ((mFinishedBitmask & finishedBit) == 0) {
40     LOGD("Stage %" PRIu32 " succeeded", stage);
41     mFinishedBitmask |= finishedBit;
42   }
43 }
44 
isAllFinished()45 bool TestSuccessMarker::isAllFinished() {
46   return (mFinishedBitmask == mAllFinished);
47 }
48 
isStageMarked(uint32_t stage)49 bool TestSuccessMarker::isStageMarked(uint32_t stage) {
50   bool marked = false;
51   if (stage <= 32) {
52     uint32_t finishedBit = (1 << stage);
53     marked = ((mFinishedBitmask & finishedBit) != 0);
54   }
55   return marked;
56 }
57 
markStageAndSuccessOnFinish(uint32_t stage)58 void TestSuccessMarker::markStageAndSuccessOnFinish(uint32_t stage) {
59   if (!isStageMarked(stage)) {
60     markStage(stage);
61     if (isAllFinished()) {
62       sendSuccessToHost();
63     }
64   }
65 }
66 
67 }  // namespace nanoapp_testing
68