File: tests_common.c

package info (click to toggle)
elektroid 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,236 kB
  • sloc: ansic: 33,475; sh: 582; makefile: 394; xml: 78
file content (247 lines) | stat: -rw-r--r-- 5,891 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <string.h>
#include <math.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include "../src/utils.h"
#include "../src/connectors/common.h"

static void
test_common_slot_get_id_from_path ()
{
  guint id;
  gint err;

  printf ("\n");

  err = common_slot_get_id_from_path ("", &id);
  CU_ASSERT_TRUE (err == -EINVAL);

  err = common_slot_get_id_from_path ("/", &id);
  CU_ASSERT_TRUE (err == -EINVAL);

  err = common_slot_get_id_from_path ("/1", &id);
  CU_ASSERT_TRUE (err == 0);
  CU_ASSERT_TRUE (id == 1);

  err = common_slot_get_id_from_path ("/p/1", &id);
  CU_ASSERT_TRUE (err == 0);
  CU_ASSERT_TRUE (id == 1);
}

static void
test_common_get_sanitized_name ()
{
  gchar *str;

  printf ("\n");

  str = common_get_sanitized_name ("asdf", NULL, '?');
  CU_ASSERT_STRING_EQUAL (str, "asdf");
  g_free (str);

  str = common_get_sanitized_name ("ásdf", NULL, '?');
  CU_ASSERT_STRING_EQUAL (str, "asdf");
  g_free (str);

  str = common_get_sanitized_name ("asdf", "asd", '?');
  CU_ASSERT_STRING_EQUAL (str, "asd?");
  g_free (str);

  str = common_get_sanitized_name ("ásdf", "asd", '?');
  CU_ASSERT_STRING_EQUAL (str, "asd?");
  g_free (str);

  str = common_get_sanitized_name ("ásdf", "asd", 0);
  CU_ASSERT_STRING_EQUAL (str, "asd");
  g_free (str);
}

void
test_common_to_os_sanitized_name ()
{
  gchar *str = strdup ("\\^/");

  printf ("\n");

  common_to_os_sanitized_name (str);
  CU_ASSERT_STRING_EQUAL (str, "?^?");
  g_free (str);
}

static const gchar *EXTS[] = { "ext", NULL };

static const gchar **
get_exts (struct backend *backend, const struct fs_operations *ops)
{
  return EXTS;
}

static void
test_common_slot_get_download_path ()
{
  struct backend backend;
  struct fs_operations ops;
  gchar *path;
  struct idata idata;

  snprintf (backend.name, LABEL_MAX, "Dev Name");
  ops.name = "fsname";
  ops.get_exts = get_exts;

  idata_init (&idata, NULL, strdup ("name"), NULL, NULL);

  path = common_slot_get_download_path_n (&backend, &ops, "/dst_dir", "/1",
					  &idata);
  CU_ASSERT_STRING_EQUAL ("/dst_dir/Dev Name fsname 1 - name.ext", path);
  g_free (path);

  path = common_slot_get_download_path_nn (&backend, &ops, "/dst_dir", "/1",
					   &idata);
  CU_ASSERT_STRING_EQUAL ("/dst_dir/Dev Name fsname 01 - name.ext", path);
  g_free (path);

  path = common_slot_get_download_path_nnn (&backend, &ops, "/dst_dir", "/1",
					    &idata);
  CU_ASSERT_STRING_EQUAL ("/dst_dir/Dev Name fsname 001 - name.ext", path);
  g_free (path);

  path = common_slot_get_download_path (&backend, &ops, "/dst_dir", "/1",
					&idata, 0);
  printf ("%s\n", path);
  CU_ASSERT_STRING_EQUAL ("/dst_dir/Dev Name fsname - name.ext", path);
  g_free (path);

  idata_clear (&idata);

  idata_init (&idata, NULL, NULL, NULL, NULL);

  path = common_slot_get_download_path_n (&backend, &ops, "/dst_dir", "/1",
					  &idata);
  CU_ASSERT_STRING_EQUAL ("/dst_dir/Dev Name fsname 1.ext", path);
  g_free (path);

  idata_clear (&idata);
}

static void
test_8bit_conversions ()
{
  guint8 *v;
  guint8 src[119], dst[136], bak[119];

  printf ("\n");

  v = src;
  for (gint i = 0; i < 32; i++, v++)
    {
      *v = g_random_int () & 0xff;
    }
  src[0] |= 0x80;		//Ensures MSB at 1.
  src[31] |= 0x80;		//Ensures MSB at 1.

  common_8bit_msg_to_midi_msg (src, dst, 32);
  common_midi_msg_to_8bit_msg (dst, bak, 37);
  CU_ASSERT_EQUAL (memcmp (src, bak, 32), 0);

  v = src;
  for (gint i = 0; i < 119; i++, v++)
    {
      *v = g_random_int () & 0xff;
    }
  src[0] |= 0x80;		//Ensures MSB at 1.
  src[118] |= 0x80;		//Ensures MSB at 1.

  common_8bit_msg_to_midi_msg (src, dst, 119);
  common_midi_msg_to_8bit_msg (dst, bak, 136);
  CU_ASSERT_EQUAL (memcmp (src, bak, 119), 0);
}

static void
test_common_8bit_msg_to_midi_msg_size ()
{
  CU_ASSERT_EQUAL (common_8bit_msg_to_midi_msg_size (0), 0);
  CU_ASSERT_EQUAL (common_8bit_msg_to_midi_msg_size (32), 37);
  CU_ASSERT_EQUAL (common_8bit_msg_to_midi_msg_size (119), 136);
  CU_ASSERT_EQUAL (common_8bit_msg_to_midi_msg_size (14420), 16480);
  CU_ASSERT_EQUAL (common_8bit_msg_to_midi_msg_size (917494), 1048565);
}

static void
test_common_midi_msg_to_8bit_msg_size ()
{
  CU_ASSERT_EQUAL (common_midi_msg_to_8bit_msg_size (0), 0);
  CU_ASSERT_EQUAL (common_midi_msg_to_8bit_msg_size (37), 32);
  CU_ASSERT_EQUAL (common_midi_msg_to_8bit_msg_size (136), 119);
  CU_ASSERT_EQUAL (common_midi_msg_to_8bit_msg_size (16480), 14420);
  CU_ASSERT_EQUAL (common_midi_msg_to_8bit_msg_size (1048565), 917494);
}

gint
main (gint argc, gchar *argv[])
{
  gint err = 0;

  debug_level = 5;

  if (CU_initialize_registry () != CUE_SUCCESS)
    {
      goto cleanup;
    }
  CU_pSuite suite = CU_add_suite ("Elektroid common connector tests", 0, 0);
  if (!suite)
    {
      goto cleanup;
    }

  if (!CU_add_test (suite, "common_slot_get_id_from_path",
		    test_common_slot_get_id_from_path))
    {
      goto cleanup;
    }

  if (!CU_add_test (suite, "common_get_sanitized_name",
		    test_common_get_sanitized_name))
    {
      goto cleanup;
    }

  if (!CU_add_test (suite, "test_common_to_os_sanitized_name",
		    test_common_to_os_sanitized_name))
    {
      goto cleanup;
    }

  if (!CU_add_test (suite, "common_slot_get_download_path",
		    test_common_slot_get_download_path))
    {
      goto cleanup;
    }

  if (!CU_add_test (suite, "8bit_conversions", test_8bit_conversions))
    {
      goto cleanup;
    }

  if (!CU_add_test
      (suite, "common_8bit_msg_to_midi_msg_size",
       test_common_8bit_msg_to_midi_msg_size))
    {
      goto cleanup;
    }

  if (!CU_add_test
      (suite, "common_midi_msg_to_8bit_msg_size",
       test_common_midi_msg_to_8bit_msg_size))
    {
      goto cleanup;
    }

  CU_basic_set_mode (CU_BRM_VERBOSE);

  CU_basic_run_tests ();
  err = CU_get_number_of_tests_failed ();

cleanup:
  CU_cleanup_registry ();
  return err || CU_get_error ();
}