b915ad068be4f26ba8da69e54c47c1da024c5062
[rtaudio-cdist.git] / tests / apinames.cpp
1 /******************************************/
2 /*
3   apinames.cpp
4   by Jean Pierre Cimalando, 2018.
5
6   This program tests parts of RtAudio related
7   to API names, the conversion from name to API
8   and vice-versa.
9 */
10 /******************************************/
11
12 #include "RtAudio.h"
13 #include <cstdlib>
14 #include <cctype>
15 #include <iostream>
16
17 int main() {
18     std::vector<RtAudio::Api> apis;
19     RtAudio::getCompiledApi( apis );
20
21     // ensure the known APIs return valid names
22     std::cout << "API names by identifier:\n";
23     for ( size_t i = 0; i < apis.size() ; ++i ) {
24         const std::string &name = RtAudio::getCompiledApiName(apis[i]);
25         if (name.empty()) {
26             std::cerr << "Invalid name for API " << (int)apis[i] << "\n";
27             exit(1);
28         }
29         std::cout << "* " << (int)apis[i] << ": '" << name << "'\n";
30     }
31
32     // ensure unknown APIs return the empty string
33     {
34         const std::string &name = RtAudio::getCompiledApiName((RtAudio::Api)-1);
35         if (!name.empty()) {
36             std::cerr << "Bad string for invalid API\n";
37             exit(1);
38         }
39     }
40
41     // try getting API identifier by case-insensitive name
42     std::cout << "API identifiers by name:\n";
43     for ( size_t i = 0; i < apis.size() ; ++i ) {
44         std::string name = RtAudio::getCompiledApiName(apis[i]);
45         if ( RtAudio::getCompiledApiByName(name) != apis[i] ) {
46             std::cerr << "Bad identifier for API '" << name << "'\n";
47             exit( 1 );
48         }
49         std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
50         for ( size_t j = 0; j < name.size(); ++j )
51             name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]);
52         if ( RtAudio::getCompiledApiByName(name) != apis[i] ) {
53             std::cerr << "Bad identifier for API '" << name << "'\n";
54             exit( 1 );
55         }
56         std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
57     }
58
59     // try getting an API identifier by unknown name
60     {
61         RtAudio::Api api;
62         api = RtAudio::getCompiledApiByName("ALSO");
63         if ( api != RtAudio::UNSPECIFIED ) {
64             std::cerr << "Bad identifier for unknown API name\n";
65             exit( 1 );
66         }
67         api = RtAudio::getCompiledApiByName("");
68         if ( api != RtAudio::UNSPECIFIED ) {
69             std::cerr << "Bad identifier for unknown API name\n";
70             exit( 1 );
71         }
72     }
73
74     return 0;
75 }