9dc634c408405e629433c92834b5bf3203a39ae1
[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 <cctype>
14 #include <cstdlib>
15 #include <iostream>
16
17 int test_cpp() {
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 (C++):\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         const std::string displayName = RtAudio::getCompiledApiDisplayName(apis[i]);
30         if (displayName.empty()) {
31             std::cerr << "Invalid display name for API " << (int)apis[i] << "\n";
32             exit(1);
33         }
34         std::cout << "* " << (int)apis[i] << " '" << name << "': '" << displayName << "'\n";
35     }
36
37     // ensure unknown APIs return the empty string
38     {
39         const std::string name = RtAudio::getCompiledApiName((RtAudio::Api)-1);
40         if (!name.empty()) {
41             std::cerr << "Bad string for invalid API '" << name << "'\n";
42             exit(1);
43         }
44         const std::string displayName = RtAudio::getCompiledApiDisplayName((RtAudio::Api)-1);
45         if (displayName!="Unknown") {
46             std::cerr << "Bad display string for invalid API '" << displayName << "'\n";
47             exit(1);
48         }
49     }
50
51     // try getting API identifier by name
52     std::cout << "API identifiers by name (C++):\n";
53     for ( size_t i = 0; i < apis.size() ; ++i ) {
54         std::string name = RtAudio::getCompiledApiName(apis[i]);
55         if ( RtAudio::getCompiledApiByName(name) != apis[i] ) {
56             std::cerr << "Bad identifier for API '" << name << "'\n";
57             exit( 1 );
58         }
59         std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
60
61         for ( size_t j = 0; j < name.size(); ++j )
62             name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]);
63         RtAudio::Api api = RtAudio::getCompiledApiByName(name);
64         if ( api != RtAudio::UNSPECIFIED ) {
65             std::cerr << "Identifier " << (int)api << " for invalid API '" << name << "'\n";
66             exit( 1 );
67         }
68     }
69
70     // try getting an API identifier by unknown name
71     {
72         RtAudio::Api api;
73         api = RtAudio::getCompiledApiByName("");
74         if ( api != RtAudio::UNSPECIFIED ) {
75             std::cerr << "Bad identifier for unknown API name\n";
76             exit( 1 );
77         }
78     }
79
80     return 0;
81 }
82
83 #include "rtaudio_c.h"
84
85 int test_c() {
86     const rtaudio_api_t *apis = rtaudio_compiled_api();
87
88     // ensure the known APIs return valid names
89     std::cout << "API names by identifier (C):\n";
90     for ( size_t i = 0; apis[i] != RTAUDIO_API_UNSPECIFIED; ++i) {
91         const std::string name = rtaudio_compiled_api_name(apis[i]);
92         if (name.empty()) {
93             std::cerr << "Invalid name for API " << (int)apis[i] << "\n";
94             exit(1);
95         }
96         const std::string displayName = rtaudio_compiled_api_display_name(apis[i]);
97         if (displayName.empty()) {
98             std::cerr << "Invalid display name for API " << (int)apis[i] << "\n";
99             exit(1);
100         }
101         std::cout << "* " << (int)apis[i] << " '" << name << "': '" << displayName << "'\n";
102     }
103
104     // ensure unknown APIs return the empty string
105     {
106         const char *s = rtaudio_compiled_api_name((rtaudio_api_t)-1);
107         const std::string name(s?s:"");
108         if (!name.empty()) {
109             std::cerr << "Bad string for invalid API '" << name << "'\n";
110             exit(1);
111         }
112         s = rtaudio_compiled_api_display_name((rtaudio_api_t)-1);
113         const std::string displayName(s?s:"");
114         if (displayName!="Unknown") {
115             std::cerr << "Bad display string for invalid API '" << displayName << "'\n";
116             exit(1);
117         }
118     }
119
120     // try getting API identifier by name
121     std::cout << "API identifiers by name (C):\n";
122     for ( size_t i = 0; apis[i] != RTAUDIO_API_UNSPECIFIED ; ++i ) {
123         const char *s = rtaudio_compiled_api_name(apis[i]);
124         std::string name(s?s:"");
125         if ( rtaudio_compiled_api_by_name(name.c_str()) != apis[i] ) {
126             std::cerr << "Bad identifier for API '" << name << "'\n";
127             exit( 1 );
128         }
129         std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
130
131         for ( size_t j = 0; j < name.size(); ++j )
132             name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]);
133         rtaudio_api_t api = rtaudio_compiled_api_by_name(name.c_str());
134         if ( api != RTAUDIO_API_UNSPECIFIED ) {
135             std::cerr << "Identifier " << (int)api << " for invalid API '" << name << "'\n";
136             exit( 1 );
137         }
138     }
139
140     // try getting an API identifier by unknown name
141     {
142         rtaudio_api_t api;
143         api = rtaudio_compiled_api_by_name("");
144         if ( api != RTAUDIO_API_UNSPECIFIED ) {
145             std::cerr << "Bad identifier for unknown API name\n";
146             exit( 1 );
147         }
148     }
149
150     return 0;
151 }
152
153 int main()
154 {
155     test_cpp();
156     test_c();
157 }