1 /*
2  * Copyright (C) 2016 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 // Unit tests for AAudio Handle Tracker
18 
19 #include <stdlib.h>
20 #include <math.h>
21 
22 #include <gtest/gtest.h>
23 
24 #include <aaudio/AAudio.h>
25 #include "utility/HandleTracker.h"
26 
27 // Test adding one address.
TEST(test_handle_tracker,aaudio_handle_tracker)28 TEST(test_handle_tracker, aaudio_handle_tracker) {
29     const int MAX_HANDLES = 4;
30     HandleTracker tracker(MAX_HANDLES);
31     handle_tracker_type_t type = 3; // arbitrary generic type
32     int data; // something that has an address we can use
33     handle_tracker_address_t found;
34 
35     // repeat the test several times to see if it breaks
36     const int SEVERAL = 5; // arbitrary
37     for (int i = 0; i < SEVERAL; i++) {
38         // should fail to find a bogus handle
39         found = tracker.get(type, 0);  // bad handle
40         EXPECT_EQ(nullptr, found);
41 
42         // create a valid handle and use it to lookup the object again
43         aaudio_handle_t dataHandle = tracker.put(type, &data);
44         ASSERT_TRUE(dataHandle > 0);
45         found = tracker.get(type, dataHandle);
46         EXPECT_EQ(&data, found);
47         found = tracker.get(type, 0); // bad handle
48         EXPECT_EQ(nullptr, found);
49 
50         // wrong type
51         found = tracker.get(type+1, dataHandle);
52         EXPECT_EQ(nullptr, found);
53 
54         // remove from storage
55         found = tracker.remove(type, dataHandle);
56         EXPECT_EQ(&data, found);
57         // should fail the second time
58         found = tracker.remove(type, dataHandle);
59         EXPECT_EQ(nullptr, found);
60     }
61 }
62 
63 // Test filling the tracker.
TEST(test_handle_tracker,aaudio_full_up)64 TEST(test_handle_tracker, aaudio_full_up) {
65     const int MAX_HANDLES = 5;
66     HandleTracker tracker(MAX_HANDLES);
67     handle_tracker_type_t type = 4; // arbitrary generic type
68     int data[MAX_HANDLES];
69     aaudio_handle_t handles[MAX_HANDLES];
70     handle_tracker_address_t found;
71 
72     // repeat the test several times to see if it breaks
73     const int SEVERAL = 5; // arbitrary
74     for (int i = 0; i < SEVERAL; i++) {
75         for (int i = 0; i < MAX_HANDLES; i++) {
76             // add a handle
77             handles[i] = tracker.put(type, &data[i]);
78             ASSERT_TRUE(handles[i] > 0);
79             found = tracker.get(type, handles[i]);
80             EXPECT_EQ(&data[i], found);
81         }
82 
83         // Now that it is full, try to add one more.
84         aaudio_handle_t handle = tracker.put(type, &data[0]);
85         EXPECT_TRUE(handle < 0);
86 
87         for (int i = 0; i < MAX_HANDLES; i++) {
88             // look up each handle
89             found = tracker.get(type, handles[i]);
90             EXPECT_EQ(&data[i], found);
91         }
92 
93         // remove one from storage
94         found = tracker.remove(type, handles[2]);
95         EXPECT_EQ(&data[2], found);
96         // now try to look up the same handle and fail
97         found = tracker.get(type, handles[2]);
98         EXPECT_EQ(nullptr, found);
99 
100         // add that same one back
101         handle = tracker.put(type, &data[2]);
102         ASSERT_TRUE(handle > 0);
103         found = tracker.get(type, handle);
104         EXPECT_EQ(&data[2], found);
105         // now use a stale handle again with a valid index and fail
106         found = tracker.get(type, handles[2]);
107         EXPECT_EQ(nullptr, found);
108 
109         // remove them all
110         handles[2] = handle;
111         for (int i = 0; i < MAX_HANDLES; i++) {
112             // look up each handle
113             found = tracker.remove(type, handles[i]);
114             EXPECT_EQ(&data[i], found);
115         }
116     }
117 }
118