sound_kunit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Sound core KUnit test
  4. * Author: Ivan Orlov <ivan.orlov0322@gmail.com>
  5. */
  6. #include <kunit/test.h>
  7. #include <sound/core.h>
  8. #include <sound/pcm.h>
  9. #define SILENCE_BUFFER_MAX_FRAMES 260
  10. #define SILENCE_BUFFER_SIZE (sizeof(u64) * SILENCE_BUFFER_MAX_FRAMES)
  11. #define SILENCE(...) { __VA_ARGS__ }
  12. #define DEFINE_FORMAT(fmt, pbits, wd, endianness, signd, silence_arr) { \
  13. .format = SNDRV_PCM_FORMAT_##fmt, .physical_bits = pbits, \
  14. .width = wd, .le = endianness, .sd = signd, .silence = silence_arr, \
  15. .name = #fmt, \
  16. }
  17. #define WRONG_FORMAT_1 (__force snd_pcm_format_t)((__force int)SNDRV_PCM_FORMAT_LAST + 1)
  18. #define WRONG_FORMAT_2 (__force snd_pcm_format_t)-1
  19. #define VALID_NAME "ValidName"
  20. #define NAME_W_SPEC_CHARS "In%v@1id name"
  21. #define NAME_W_SPACE "Test name"
  22. #define NAME_W_SPACE_REMOVED "Testname"
  23. #define TEST_FIRST_COMPONENT "Component1"
  24. #define TEST_SECOND_COMPONENT "Component2"
  25. struct snd_format_test_data {
  26. snd_pcm_format_t format;
  27. int physical_bits;
  28. int width;
  29. int le;
  30. int sd;
  31. unsigned char silence[8];
  32. unsigned char *name;
  33. };
  34. struct avail_test_data {
  35. snd_pcm_uframes_t buffer_size;
  36. snd_pcm_uframes_t hw_ptr;
  37. snd_pcm_uframes_t appl_ptr;
  38. snd_pcm_uframes_t expected_avail;
  39. };
  40. static const struct snd_format_test_data valid_fmt[] = {
  41. DEFINE_FORMAT(S8, 8, 8, -1, 1, SILENCE()),
  42. DEFINE_FORMAT(U8, 8, 8, -1, 0, SILENCE(0x80)),
  43. DEFINE_FORMAT(S16_LE, 16, 16, 1, 1, SILENCE()),
  44. DEFINE_FORMAT(S16_BE, 16, 16, 0, 1, SILENCE()),
  45. DEFINE_FORMAT(U16_LE, 16, 16, 1, 0, SILENCE(0x00, 0x80)),
  46. DEFINE_FORMAT(U16_BE, 16, 16, 0, 0, SILENCE(0x80, 0x00)),
  47. DEFINE_FORMAT(S24_LE, 32, 24, 1, 1, SILENCE()),
  48. DEFINE_FORMAT(S24_BE, 32, 24, 0, 1, SILENCE()),
  49. DEFINE_FORMAT(U24_LE, 32, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)),
  50. DEFINE_FORMAT(U24_BE, 32, 24, 0, 0, SILENCE(0x00, 0x80, 0x00, 0x00)),
  51. DEFINE_FORMAT(S32_LE, 32, 32, 1, 1, SILENCE()),
  52. DEFINE_FORMAT(S32_BE, 32, 32, 0, 1, SILENCE()),
  53. DEFINE_FORMAT(U32_LE, 32, 32, 1, 0, SILENCE(0x00, 0x00, 0x00, 0x80)),
  54. DEFINE_FORMAT(U32_BE, 32, 32, 0, 0, SILENCE(0x80, 0x00, 0x00, 0x00)),
  55. DEFINE_FORMAT(FLOAT_LE, 32, 32, 1, -1, SILENCE()),
  56. DEFINE_FORMAT(FLOAT_BE, 32, 32, 0, -1, SILENCE()),
  57. DEFINE_FORMAT(FLOAT64_LE, 64, 64, 1, -1, SILENCE()),
  58. DEFINE_FORMAT(FLOAT64_BE, 64, 64, 0, -1, SILENCE()),
  59. DEFINE_FORMAT(IEC958_SUBFRAME_LE, 32, 32, 1, -1, SILENCE()),
  60. DEFINE_FORMAT(IEC958_SUBFRAME_BE, 32, 32, 0, -1, SILENCE()),
  61. DEFINE_FORMAT(MU_LAW, 8, 8, -1, -1, SILENCE(0x7f)),
  62. DEFINE_FORMAT(A_LAW, 8, 8, -1, -1, SILENCE(0x55)),
  63. DEFINE_FORMAT(IMA_ADPCM, 4, 4, -1, -1, SILENCE()),
  64. DEFINE_FORMAT(G723_24, 3, 3, -1, -1, SILENCE()),
  65. DEFINE_FORMAT(G723_40, 5, 5, -1, -1, SILENCE()),
  66. DEFINE_FORMAT(DSD_U8, 8, 8, 1, 0, SILENCE(0x69)),
  67. DEFINE_FORMAT(DSD_U16_LE, 16, 16, 1, 0, SILENCE(0x69, 0x69)),
  68. DEFINE_FORMAT(DSD_U32_LE, 32, 32, 1, 0, SILENCE(0x69, 0x69, 0x69, 0x69)),
  69. DEFINE_FORMAT(DSD_U16_BE, 16, 16, 0, 0, SILENCE(0x69, 0x69)),
  70. DEFINE_FORMAT(DSD_U32_BE, 32, 32, 0, 0, SILENCE(0x69, 0x69, 0x69, 0x69)),
  71. DEFINE_FORMAT(S20_LE, 32, 20, 1, 1, SILENCE()),
  72. DEFINE_FORMAT(S20_BE, 32, 20, 0, 1, SILENCE()),
  73. DEFINE_FORMAT(U20_LE, 32, 20, 1, 0, SILENCE(0x00, 0x00, 0x08, 0x00)),
  74. DEFINE_FORMAT(U20_BE, 32, 20, 0, 0, SILENCE(0x00, 0x08, 0x00, 0x00)),
  75. DEFINE_FORMAT(S24_3LE, 24, 24, 1, 1, SILENCE()),
  76. DEFINE_FORMAT(S24_3BE, 24, 24, 0, 1, SILENCE()),
  77. DEFINE_FORMAT(U24_3LE, 24, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)),
  78. DEFINE_FORMAT(U24_3BE, 24, 24, 0, 0, SILENCE(0x80, 0x00, 0x00)),
  79. DEFINE_FORMAT(S20_3LE, 24, 20, 1, 1, SILENCE()),
  80. DEFINE_FORMAT(S20_3BE, 24, 20, 0, 1, SILENCE()),
  81. DEFINE_FORMAT(U20_3LE, 24, 20, 1, 0, SILENCE(0x00, 0x00, 0x08)),
  82. DEFINE_FORMAT(U20_3BE, 24, 20, 0, 0, SILENCE(0x08, 0x00, 0x00)),
  83. DEFINE_FORMAT(S18_3LE, 24, 18, 1, 1, SILENCE()),
  84. DEFINE_FORMAT(S18_3BE, 24, 18, 0, 1, SILENCE()),
  85. DEFINE_FORMAT(U18_3LE, 24, 18, 1, 0, SILENCE(0x00, 0x00, 0x02)),
  86. DEFINE_FORMAT(U18_3BE, 24, 18, 0, 0, SILENCE(0x02, 0x00, 0x00)),
  87. DEFINE_FORMAT(G723_24_1B, 8, 3, -1, -1, SILENCE()),
  88. DEFINE_FORMAT(G723_40_1B, 8, 5, -1, -1, SILENCE()),
  89. };
  90. static void test_phys_format_size(struct kunit *test)
  91. {
  92. u32 i;
  93. for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
  94. KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(valid_fmt[i].format),
  95. valid_fmt[i].physical_bits);
  96. }
  97. KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_1), -EINVAL);
  98. KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_2), -EINVAL);
  99. }
  100. static void test_format_width(struct kunit *test)
  101. {
  102. u32 i;
  103. for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
  104. KUNIT_EXPECT_EQ(test, snd_pcm_format_width(valid_fmt[i].format),
  105. valid_fmt[i].width);
  106. }
  107. KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
  108. KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
  109. }
  110. static void test_format_signed(struct kunit *test)
  111. {
  112. u32 i;
  113. for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
  114. KUNIT_EXPECT_EQ(test, snd_pcm_format_signed(valid_fmt[i].format),
  115. valid_fmt[i].sd < 0 ? -EINVAL : valid_fmt[i].sd);
  116. KUNIT_EXPECT_EQ(test, snd_pcm_format_unsigned(valid_fmt[i].format),
  117. valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd);
  118. }
  119. KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
  120. KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
  121. }
  122. static void test_format_endianness(struct kunit *test)
  123. {
  124. u32 i;
  125. for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
  126. KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(valid_fmt[i].format),
  127. valid_fmt[i].le < 0 ? -EINVAL : valid_fmt[i].le);
  128. KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(valid_fmt[i].format),
  129. valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le);
  130. }
  131. KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_1), -EINVAL);
  132. KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_2), -EINVAL);
  133. KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_1), -EINVAL);
  134. KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_2), -EINVAL);
  135. }
  136. static void _test_fill_silence(struct kunit *test, const struct snd_format_test_data *data,
  137. u8 *buffer, size_t samples_count)
  138. {
  139. size_t sample_bytes = data->physical_bits >> 3;
  140. u32 i;
  141. KUNIT_ASSERT_EQ(test, snd_pcm_format_set_silence(data->format, buffer, samples_count), 0);
  142. for (i = 0; i < samples_count * sample_bytes; i++)
  143. KUNIT_EXPECT_EQ(test, buffer[i], data->silence[i % sample_bytes]);
  144. }
  145. static void test_format_fill_silence(struct kunit *test)
  146. {
  147. static const u32 buf_samples[] = { 10, 20, 32, 64, 129, SILENCE_BUFFER_MAX_FRAMES };
  148. u8 *buffer;
  149. u32 i, j;
  150. buffer = kunit_kzalloc(test, SILENCE_BUFFER_SIZE, GFP_KERNEL);
  151. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer);
  152. for (i = 0; i < ARRAY_SIZE(buf_samples); i++) {
  153. for (j = 0; j < ARRAY_SIZE(valid_fmt); j++)
  154. _test_fill_silence(test, &valid_fmt[j], buffer, buf_samples[i]);
  155. }
  156. KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT_1, buffer, 20), -EINVAL);
  157. KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0);
  158. }
  159. static snd_pcm_uframes_t calculate_boundary(snd_pcm_uframes_t buffer_size)
  160. {
  161. snd_pcm_uframes_t boundary = buffer_size;
  162. while (boundary * 2 <= 0x7fffffffUL - buffer_size)
  163. boundary *= 2;
  164. return boundary;
  165. }
  166. static const struct avail_test_data p_avail_data[] = {
  167. /* buf_size + hw_ptr < appl_ptr => avail = buf_size + hw_ptr - appl_ptr + boundary */
  168. { 128, 1000, 1129, 1073741824UL - 1 },
  169. /*
  170. * buf_size + hw_ptr - appl_ptr >= boundary =>
  171. * => avail = buf_size + hw_ptr - appl_ptr - boundary
  172. */
  173. { 128, 1073741824UL, 10, 118 },
  174. /* standard case: avail = buf_size + hw_ptr - appl_ptr */
  175. { 128, 1000, 1001, 127 },
  176. };
  177. static void test_playback_avail(struct kunit *test)
  178. {
  179. struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
  180. u32 i;
  181. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r);
  182. r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
  183. r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
  184. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->status);
  185. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->control);
  186. for (i = 0; i < ARRAY_SIZE(p_avail_data); i++) {
  187. r->buffer_size = p_avail_data[i].buffer_size;
  188. r->boundary = calculate_boundary(r->buffer_size);
  189. r->status->hw_ptr = p_avail_data[i].hw_ptr;
  190. r->control->appl_ptr = p_avail_data[i].appl_ptr;
  191. KUNIT_EXPECT_EQ(test, snd_pcm_playback_avail(r), p_avail_data[i].expected_avail);
  192. }
  193. }
  194. static const struct avail_test_data c_avail_data[] = {
  195. /* hw_ptr - appl_ptr < 0 => avail = hw_ptr - appl_ptr + boundary */
  196. { 128, 1000, 1001, 1073741824UL - 1 },
  197. /* standard case: avail = hw_ptr - appl_ptr */
  198. { 128, 1001, 1000, 1 },
  199. };
  200. static void test_capture_avail(struct kunit *test)
  201. {
  202. struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
  203. u32 i;
  204. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r);
  205. r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
  206. r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
  207. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->status);
  208. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->control);
  209. for (i = 0; i < ARRAY_SIZE(c_avail_data); i++) {
  210. r->buffer_size = c_avail_data[i].buffer_size;
  211. r->boundary = calculate_boundary(r->buffer_size);
  212. r->status->hw_ptr = c_avail_data[i].hw_ptr;
  213. r->control->appl_ptr = c_avail_data[i].appl_ptr;
  214. KUNIT_EXPECT_EQ(test, snd_pcm_capture_avail(r), c_avail_data[i].expected_avail);
  215. }
  216. }
  217. static void test_card_set_id(struct kunit *test)
  218. {
  219. struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
  220. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, card);
  221. snd_card_set_id(card, VALID_NAME);
  222. KUNIT_EXPECT_STREQ(test, card->id, VALID_NAME);
  223. /* clear the first id character so we can set it again */
  224. card->id[0] = '\0';
  225. snd_card_set_id(card, NAME_W_SPEC_CHARS);
  226. KUNIT_EXPECT_STRNEQ(test, card->id, NAME_W_SPEC_CHARS);
  227. card->id[0] = '\0';
  228. snd_card_set_id(card, NAME_W_SPACE);
  229. kunit_info(test, "%s", card->id);
  230. KUNIT_EXPECT_STREQ(test, card->id, NAME_W_SPACE_REMOVED);
  231. }
  232. static void test_pcm_format_name(struct kunit *test)
  233. {
  234. u32 i;
  235. const char *name;
  236. for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
  237. name = snd_pcm_format_name(valid_fmt[i].format);
  238. KUNIT_ASSERT_NOT_NULL_MSG(test, name, "Don't have name for %s", valid_fmt[i].name);
  239. KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name);
  240. }
  241. KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_1), "Unknown");
  242. KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_2), "Unknown");
  243. }
  244. static void test_card_add_component(struct kunit *test)
  245. {
  246. struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
  247. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, card);
  248. snd_component_add(card, TEST_FIRST_COMPONENT);
  249. KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT);
  250. snd_component_add(card, TEST_SECOND_COMPONENT);
  251. KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT " " TEST_SECOND_COMPONENT);
  252. }
  253. static struct kunit_case sound_utils_cases[] = {
  254. KUNIT_CASE(test_phys_format_size),
  255. KUNIT_CASE(test_format_width),
  256. KUNIT_CASE(test_format_endianness),
  257. KUNIT_CASE(test_format_signed),
  258. KUNIT_CASE(test_format_fill_silence),
  259. KUNIT_CASE(test_playback_avail),
  260. KUNIT_CASE(test_capture_avail),
  261. KUNIT_CASE(test_card_set_id),
  262. KUNIT_CASE(test_pcm_format_name),
  263. KUNIT_CASE(test_card_add_component),
  264. {},
  265. };
  266. static struct kunit_suite sound_utils_suite = {
  267. .name = "sound-core-test",
  268. .test_cases = sound_utils_cases,
  269. };
  270. kunit_test_suite(sound_utils_suite);
  271. MODULE_DESCRIPTION("Sound core KUnit test");
  272. MODULE_AUTHOR("Ivan Orlov");
  273. MODULE_LICENSE("GPL");