source: mainline/uspace/lib/riff/test/rwave.c@ 95658c9

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 95658c9 was d145ecb, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Libriff needs unit tests

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <mem.h>
30#include <pcut/pcut.h>
31#include <riff/rwave.h>
32#include <stdio.h>
33#include <stdint.h>
34
35PCUT_INIT;
36
37PCUT_TEST_SUITE(rwave);
38
39/** Write a WAVE file and read it back */
40PCUT_TEST(write_read)
41{
42 char fname[L_tmpnam];
43 char *p;
44 uint16_t samples[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
45 uint16_t rsamples[10];
46 size_t nread;
47 rwavew_t *ww = NULL;
48 rwaver_t *wr = NULL;
49 rwave_params_t params;
50 rwave_params_t rparams;
51 errno_t rc;
52
53 p = tmpnam(fname);
54 PCUT_ASSERT_NOT_NULL(p);
55
56 /* Write RIFF wAVE file */
57
58 params.channels = 2;
59 params.bits_smp = 16;
60 params.smp_freq = 44100;
61
62 rc = rwave_wopen(p, &params, &ww);
63 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
64 PCUT_ASSERT_NOT_NULL(ww);
65
66 rc = rwave_write_samples(ww, (void *) samples, sizeof(samples));
67 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
68
69 rc = rwave_wclose(ww);
70 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
71
72 /* Read back RIFF WAVE file */
73
74 rc = rwave_ropen(p, &rparams, &wr);
75 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
76 PCUT_ASSERT_NOT_NULL(wr);
77
78 PCUT_ASSERT_INT_EQUALS(params.channels, rparams.channels);
79 PCUT_ASSERT_INT_EQUALS(params.bits_smp, rparams.bits_smp);
80 PCUT_ASSERT_INT_EQUALS(params.smp_freq, rparams.smp_freq);
81
82 rc = rwave_read_samples(wr, rsamples, sizeof(rsamples), &nread);
83 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
84 PCUT_ASSERT_INT_EQUALS(sizeof(samples), nread);
85
86 PCUT_ASSERT_INT_EQUALS(0, memcmp(samples, rsamples, sizeof(samples)));
87
88 rc = rwave_rclose(wr);
89 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
90
91 (void) remove(p);
92}
93
94PCUT_EXPORT(rwave);
Note: See TracBrowser for help on using the repository browser.