1e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk/*
2e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * Copyright (C) 2016 The Android Open Source Project
3e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *
4e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * you may not use this file except in compliance with the License.
6e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * You may obtain a copy of the License at
7e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *
8e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *
10e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * Unless required by applicable law or agreed to in writing, software
11e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * See the License for the specific language governing permissions and
14e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * limitations under the License.
15e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk */
16e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// Unit tests for AAudio Handle Tracker
18e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
19e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk#include <stdlib.h>
20e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk#include <math.h>
21e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
22e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk#include <gtest/gtest.h>
23e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
24a4eb0d86a29be2763be5fac51727858d5095794bPhil Burk#include <aaudio/AAudio.h>
25e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#include "utility/HandleTracker.h"
26e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
27e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk// Test adding one address.
285ed503c7a66c90f93759c90237a9b432dbd93f9fPhil BurkTEST(test_handle_tracker, aaudio_handle_tracker) {
29e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    const int MAX_HANDLES = 4;
30e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    HandleTracker tracker(MAX_HANDLES);
31e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    handle_tracker_type_t type = 3; // arbitrary generic type
32e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    int data; // something that has an address we can use
33e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    handle_tracker_address_t found;
34e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
35e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // repeat the test several times to see if it breaks
36e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    const int SEVERAL = 5; // arbitrary
37e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    for (int i = 0; i < SEVERAL; i++) {
38e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // should fail to find a bogus handle
39e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.get(type, 0);  // bad handle
40e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(nullptr, found);
41e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
42e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // create a valid handle and use it to lookup the object again
435ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        aaudio_handle_t dataHandle = tracker.put(type, &data);
44e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        ASSERT_TRUE(dataHandle > 0);
45e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.get(type, dataHandle);
46e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(&data, found);
47e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.get(type, 0); // bad handle
48e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(nullptr, found);
49e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
50e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // wrong type
51e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.get(type+1, dataHandle);
52e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(nullptr, found);
53e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
54e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // remove from storage
55e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.remove(type, dataHandle);
56e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(&data, found);
57e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // should fail the second time
58e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.remove(type, dataHandle);
59d0ffd62e89bf97fa20a63f282d034b6745328210Phil Burk        EXPECT_EQ(nullptr, found);
60e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
61e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk}
62e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
63e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk// Test filling the tracker.
645ed503c7a66c90f93759c90237a9b432dbd93f9fPhil BurkTEST(test_handle_tracker, aaudio_full_up) {
65e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    const int MAX_HANDLES = 5;
66e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    HandleTracker tracker(MAX_HANDLES);
67e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    handle_tracker_type_t type = 4; // arbitrary generic type
68e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    int data[MAX_HANDLES];
695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_handle_t handles[MAX_HANDLES];
70e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    handle_tracker_address_t found;
71e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
72e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // repeat the test several times to see if it breaks
73e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    const int SEVERAL = 5; // arbitrary
74e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    for (int i = 0; i < SEVERAL; i++) {
75e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        for (int i = 0; i < MAX_HANDLES; i++) {
76e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            // add a handle
77e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            handles[i] = tracker.put(type, &data[i]);
78e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            ASSERT_TRUE(handles[i] > 0);
79e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            found = tracker.get(type, handles[i]);
80e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            EXPECT_EQ(&data[i], found);
81e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        }
82e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
83e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // Now that it is full, try to add one more.
845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        aaudio_handle_t handle = tracker.put(type, &data[0]);
85e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_TRUE(handle < 0);
86e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
87e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        for (int i = 0; i < MAX_HANDLES; i++) {
88e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            // look up each handle
89e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            found = tracker.get(type, handles[i]);
90e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            EXPECT_EQ(&data[i], found);
91e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        }
92e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
93e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // remove one from storage
94e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.remove(type, handles[2]);
95e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(&data[2], found);
96e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // now try to look up the same handle and fail
97e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.get(type, handles[2]);
98e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(nullptr, found);
99e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
100e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // add that same one back
101e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        handle = tracker.put(type, &data[2]);
102e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        ASSERT_TRUE(handle > 0);
103e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.get(type, handle);
104e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(&data[2], found);
105e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // now use a stale handle again with a valid index and fail
106e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        found = tracker.get(type, handles[2]);
107e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        EXPECT_EQ(nullptr, found);
108e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
109e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        // remove them all
110e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        handles[2] = handle;
111e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        for (int i = 0; i < MAX_HANDLES; i++) {
112e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            // look up each handle
113e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            found = tracker.remove(type, handles[i]);
114e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk            EXPECT_EQ(&data[i], found);
115e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        }
116e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
117e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk}
118