apinames.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /******************************************/
  2. /*
  3. apinames.cpp
  4. by Jean Pierre Cimalando, 2018.
  5. This program tests parts of RtMidi related
  6. to API names, the conversion from name to API
  7. and vice-versa.
  8. */
  9. /******************************************/
  10. #include "RtMidi.h"
  11. #include <cctype>
  12. #include <cstdlib>
  13. #include <iostream>
  14. int test_cpp() {
  15. std::vector<RtMidi::Api> apis;
  16. RtMidi::getCompiledApi( apis );
  17. // ensure the known APIs return valid names
  18. std::cout << "API names by identifier (C++):\n";
  19. for ( size_t i = 0; i < apis.size() ; ++i ) {
  20. const std::string name = RtMidi::getApiName(apis[i]);
  21. if (name.empty()) {
  22. std::cout << "Invalid name for API " << (int)apis[i] << "\n";
  23. exit(1);
  24. }
  25. const std::string displayName = RtMidi::getApiDisplayName(apis[i]);
  26. if (displayName.empty()) {
  27. std::cout << "Invalid display name for API " << (int)apis[i] << "\n";
  28. exit(1);
  29. }
  30. std::cout << "* " << (int)apis[i] << " '" << name << "': '" << displayName << "'\n";
  31. }
  32. // ensure unknown APIs return the empty string
  33. {
  34. const std::string name = RtMidi::getApiName((RtMidi::Api)-1);
  35. if (!name.empty()) {
  36. std::cout << "Bad string for invalid API '" << name << "'\n";
  37. exit(1);
  38. }
  39. const std::string displayName = RtMidi::getApiDisplayName((RtMidi::Api)-1);
  40. if (displayName!="Unknown") {
  41. std::cout << "Bad display string for invalid API '" << displayName << "'\n";
  42. exit(1);
  43. }
  44. }
  45. // try getting API identifier by name
  46. std::cout << "API identifiers by name (C++):\n";
  47. for ( size_t i = 0; i < apis.size() ; ++i ) {
  48. std::string name = RtMidi::getApiName(apis[i]);
  49. if ( RtMidi::getCompiledApiByName(name) != apis[i] ) {
  50. std::cout << "Bad identifier for API '" << name << "'\n";
  51. exit( 1 );
  52. }
  53. std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
  54. for ( size_t j = 0; j < name.size(); ++j )
  55. name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]);
  56. RtMidi::Api api = RtMidi::getCompiledApiByName(name);
  57. if ( api != RtMidi::UNSPECIFIED ) {
  58. std::cout << "Identifier " << (int)api << " for invalid API '" << name << "'\n";
  59. exit( 1 );
  60. }
  61. }
  62. // try getting an API identifier by unknown name
  63. {
  64. RtMidi::Api api;
  65. api = RtMidi::getCompiledApiByName("");
  66. if ( api != RtMidi::UNSPECIFIED ) {
  67. std::cout << "Bad identifier for unknown API name\n";
  68. exit( 1 );
  69. }
  70. }
  71. return 0;
  72. }
  73. #include "rtmidi_c.h"
  74. int test_c() {
  75. unsigned api_count = rtmidi_get_compiled_api(NULL, 0);
  76. std::vector<RtMidiApi> apis(api_count);
  77. rtmidi_get_compiled_api(apis.data(), api_count);
  78. // ensure the known APIs return valid names
  79. std::cout << "API names by identifier (C):\n";
  80. for ( size_t i = 0; i < api_count; ++i) {
  81. const std::string name = rtmidi_api_name(apis[i]);
  82. if (name.empty()) {
  83. std::cout << "Invalid name for API " << (int)apis[i] << "\n";
  84. exit(1);
  85. }
  86. const std::string displayName = rtmidi_api_display_name(apis[i]);
  87. if (displayName.empty()) {
  88. std::cout << "Invalid display name for API " << (int)apis[i] << "\n";
  89. exit(1);
  90. }
  91. std::cout << "* " << (int)apis[i] << " '" << name << "': '" << displayName << "'\n";
  92. }
  93. // ensure unknown APIs return the empty string
  94. {
  95. const char *s = rtmidi_api_name((RtMidiApi)-1);
  96. const std::string name(s?s:"");
  97. if (!name.empty()) {
  98. std::cout << "Bad string for invalid API '" << name << "'\n";
  99. exit(1);
  100. }
  101. s = rtmidi_api_display_name((RtMidiApi)-1);
  102. const std::string displayName(s?s:"");
  103. if (displayName!="Unknown") {
  104. std::cout << "Bad display string for invalid API '" << displayName << "'\n";
  105. exit(1);
  106. }
  107. }
  108. // try getting API identifier by name
  109. std::cout << "API identifiers by name (C):\n";
  110. for ( size_t i = 0; i < api_count ; ++i ) {
  111. const char *s = rtmidi_api_name(apis[i]);
  112. std::string name(s?s:"");
  113. if ( rtmidi_compiled_api_by_name(name.c_str()) != apis[i] ) {
  114. std::cout << "Bad identifier for API '" << name << "'\n";
  115. exit( 1 );
  116. }
  117. std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
  118. for ( size_t j = 0; j < name.size(); ++j )
  119. name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]);
  120. RtMidiApi api = rtmidi_compiled_api_by_name(name.c_str());
  121. if ( api != RTMIDI_API_UNSPECIFIED ) {
  122. std::cout << "Identifier " << (int)api << " for invalid API '" << name << "'\n";
  123. exit( 1 );
  124. }
  125. }
  126. // try getting an API identifier by unknown name
  127. {
  128. RtMidiApi api;
  129. api = rtmidi_compiled_api_by_name("");
  130. if ( api != RTMIDI_API_UNSPECIFIED ) {
  131. std::cout << "Bad identifier for unknown API name\n";
  132. exit( 1 );
  133. }
  134. }
  135. return 0;
  136. }
  137. int main()
  138. {
  139. test_cpp();
  140. test_c();
  141. }