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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
#include <string.h>
#include <math.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include "../src/utils.h"
#include "../src/connectors/scala.h"
#define TEST_MAX_FILE_LEN 1024
gint scala_init_scala_from_bytes (struct scala *scala, GByteArray * input);
static const guint8 SUCCESS_OCTAVE_MIDI_MSG[] = {
0xf0,
0x7e,
0x7f,
8, 6,
0, //bank
0, //tuning
0x35, 0x2d, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x20, 0x6a, 0x75, 0x73, 0x74, 0x20, 0x69, 0x6e, 0x74, //name
0x40, 0x00, //pitches
0x47, 0x41,
0x42, 0x40,
0x4a, 0x01,
0x37, 0x1e,
0x3e, 0x5f,
0x39, 0x5f,
0x41, 0x20,
0x48, 0x61,
0x35, 0x7e,
0x3d, 0x3f,
0x38, 0x3e,
0x2f, //cksum
0xf7
};
static const guint8 SUCCESS_BULK_MIDI_MSG_HEADER[] = {
0xf0,
0x7e,
0x7f,
8, 1,
0, //tuning
0x35, 0x2d, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x20, 0x6a, 0x75, 0x73, 0x74, 0x20, 0x69, 0x6e, 0x74 //name
};
static const guint8 SUCCESS_BULK_MIDI_MSG_OCTAVE_DATA[] = {
0x00, 0x00, 0x00,
0x01, 0x0f, 0x03,
0x02, 0x05, 0x00,
0x03, 0x14, 0x04,
0x03, 0x6e, 0x45,
0x04, 0x7d, 0x48,
0x05, 0x73, 0x46,
0x07, 0x02, 0x40,
0x08, 0x11, 0x43,
0x08, 0x6c, 0x05,
0x09, 0x7b, 0x08,
0x0a, 0x71, 0x06
};
static const guint8 SUCCESS_OCTAVE_MIDI_MSG_TET[] = {
0xf0,
0x7e,
0x7f,
8, 6,
0, //bank
0, //tuning
0x54, 0x45, 0x54, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, //name
0x40, 0x00, //pitches
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x40, 0x00,
0x6a, //cksum
0xf7
};
static const guint8 SUCCESS_BULK_MIDI_MSG_HEADER_TET[] = {
0xf0,
0x7e,
0x7f,
8, 1,
0, //tuning
0x54, 0x45, 0x54, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 //name
};
void
test_success ()
{
struct scala scala;
struct idata idata;
gint err;
printf ("\n");
file_load (TEST_DATA_DIR "/scala/success.scl", &idata, NULL);
err = scala_init_scala_from_bytes (&scala, idata.content);
idata_clear (&idata);
CU_ASSERT_EQUAL (err, 0);
CU_ASSERT_STRING_EQUAL (scala.desc, "5-limit just intonation");
CU_ASSERT_EQUAL (scala.notes, 12);
CU_ASSERT_EQUAL (scala.pitches[0], 16 / (double) 15);
CU_ASSERT_EQUAL (scala.pitches[1], 9 / (double) 8);
CU_ASSERT_EQUAL (scala.pitches[2], 6 / (double) 5);
CU_ASSERT_EQUAL (scala.pitches[3], 5 / (double) 4);
CU_ASSERT_EQUAL (scala.pitches[4], 4 / (double) 3);
CU_ASSERT_EQUAL (scala.pitches[5], 45 / (double) 32);
CU_ASSERT_EQUAL (scala.pitches[6], 3 / (double) 2);
CU_ASSERT_EQUAL (scala.pitches[7], 8 / (double) 5);
CU_ASSERT_EQUAL (scala.pitches[8], 5 / (double) 3);
CU_ASSERT_EQUAL (scala.pitches[9], 16 / (double) 9);
CU_ASSERT_EQUAL (scala.pitches[10], 15 / (double) 8);
CU_ASSERT_EQUAL (scala.pitches[11], 2 / (double) 1);
}
void
test_success_perfect_5th ()
{
struct scala scala;
struct idata idata;
gint err;
printf ("\n");
file_load (TEST_DATA_DIR "/scala/perfect_5th.scl", &idata, NULL);
err = scala_init_scala_from_bytes (&scala, idata.content);
idata_clear (&idata);
CU_ASSERT_EQUAL (err, 0);
CU_ASSERT_STRING_EQUAL (scala.desc, "Perfect 5th");
CU_ASSERT_EQUAL (scala.notes, 1);
CU_ASSERT_EQUAL (scala.pitches[0], 1.5);
}
void
test_empty_file ()
{
struct scala scala;
GByteArray *data;
gint err;
printf ("\n");
data = g_byte_array_sized_new (0);
err = scala_init_scala_from_bytes (&scala, data);
CU_ASSERT_EQUAL (err, -EINVAL);
g_byte_array_free (data, TRUE);
}
void
test_too_many_notes ()
{
struct scala scala;
struct idata idata;
gint err;
printf ("\n");
file_load (TEST_DATA_DIR "/scala/too_many_notes.scl", &idata, NULL);
err = scala_init_scala_from_bytes (&scala, idata.content);
CU_ASSERT_EQUAL (err, -ERANGE);
idata_clear (&idata);
}
void
test_no_notes ()
{
struct scala scala;
struct idata idata;
gint err;
printf ("\n");
file_load (TEST_DATA_DIR "/scala/no_notes.scl", &idata, NULL);
err = scala_init_scala_from_bytes (&scala, idata.content);
CU_ASSERT_EQUAL (err, -ERANGE);
idata_clear (&idata);
}
void
test_unmatching_notes ()
{
struct scala scala;
struct idata idata;
gint err;
printf ("\n");
file_load (TEST_DATA_DIR "/scala/unmatching_notes.scl", &idata, NULL);
err = scala_init_scala_from_bytes (&scala, idata.content);
CU_ASSERT_EQUAL (err, -EINVAL);
idata_clear (&idata);
}
void
test_get_2_byte_octave_midi_message ()
{
struct idata idata;
gint err;
printf ("\n");
err = scala_load_2_byte_octave_tuning_msg (TEST_DATA_DIR
"/scala/success.scl",
&idata, NULL);
CU_ASSERT_EQUAL (err, 0);
CU_ASSERT_EQUAL (idata.content->len, sizeof (SUCCESS_OCTAVE_MIDI_MSG));
CU_ASSERT_EQUAL (memcmp (idata.content->data, SUCCESS_OCTAVE_MIDI_MSG,
sizeof (SUCCESS_OCTAVE_MIDI_MSG)), 0);
idata_clear (&idata);
}
void
test_get_bulk_tuning_midi_message ()
{
gint err;
struct idata idata;
guint8 *b, *f_data;
printf ("\n");
err = scala_load_key_based_tuning_msg (TEST_DATA_DIR
"/scala/success.scl", &idata, NULL);
CU_ASSERT_EQUAL (err, 0);
CU_ASSERT_EQUAL (idata.content->len, SCALA_TUNING_BANK_SIZE);
CU_ASSERT_EQUAL (memcmp (idata.content->data, SUCCESS_BULK_MIDI_MSG_HEADER,
sizeof (SUCCESS_BULK_MIDI_MSG_HEADER)), 0);
//Test the first octave.
f_data = idata.content->data + sizeof (SUCCESS_BULK_MIDI_MSG_HEADER);
CU_ASSERT_EQUAL (memcmp (f_data, SUCCESS_BULK_MIDI_MSG_OCTAVE_DATA,
sizeof (SUCCESS_BULK_MIDI_MSG_OCTAVE_DATA)), 0);
//Test the nots above the first octave by comparing them to the notes in the first one.
b = f_data + 12 * 3;
for (guint8 i = 12; i < SCALA_MIDI_NOTES; i++, b += 3)
{
gint offset = (i % 12) * 3;
gint octave = (i / 12) * 12;
CU_ASSERT_EQUAL (*b, *(f_data + offset) + octave);
CU_ASSERT_EQUAL (*(b + 1), *(f_data + offset + 1));
CU_ASSERT_EQUAL (*(b + 2), *(f_data + offset + 2));
}
CU_ASSERT_EQUAL (idata.content->data[406], 0x03);
CU_ASSERT_EQUAL (idata.content->data[407], 0xf7);
idata_clear (&idata);
}
void
test_get_2_byte_octave_midi_message_test ()
{
struct idata idata;
gint err;
printf ("\n");
err = scala_load_2_byte_octave_tuning_msg (TEST_DATA_DIR
"/scala/TET.scl", &idata, NULL);
CU_ASSERT_EQUAL (err, 0);
CU_ASSERT_EQUAL (idata.content->len, sizeof (SUCCESS_OCTAVE_MIDI_MSG_TET));
CU_ASSERT_EQUAL (memcmp (idata.content->data, SUCCESS_OCTAVE_MIDI_MSG_TET,
sizeof (SUCCESS_OCTAVE_MIDI_MSG_TET)), 0);
idata_clear (&idata);
}
void
test_get_bulk_tuning_midi_message_tet ()
{
gint err;
guint8 *b;
struct idata idata;
printf ("\n");
err = scala_load_key_based_tuning_msg (TEST_DATA_DIR
"/scala/TET.scl", &idata, NULL);
CU_ASSERT_EQUAL (err, 0);
CU_ASSERT_EQUAL (idata.content->len, SCALA_TUNING_BANK_SIZE);
CU_ASSERT_EQUAL (memcmp (idata.content->data,
SUCCESS_BULK_MIDI_MSG_HEADER_TET,
sizeof (SUCCESS_BULK_MIDI_MSG_HEADER_TET)), 0);
b = idata.content->data + sizeof (SUCCESS_BULK_MIDI_MSG_HEADER_TET);
for (guint8 i = 0; i < SCALA_MIDI_NOTES; i++, b += 3)
{
CU_ASSERT_EQUAL (*b, i);
CU_ASSERT_EQUAL (*(b + 1), 0);
CU_ASSERT_EQUAL (*(b + 2), 0);
}
CU_ASSERT_EQUAL (idata.content->data[406], 0x6d);
CU_ASSERT_EQUAL (idata.content->data[407], 0xf7);
idata_clear (&idata);
}
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 scala tests", 0, 0);
if (!suite)
{
goto cleanup;
}
if (!CU_add_test (suite, "test_success", test_success))
{
goto cleanup;
}
if (!CU_add_test (suite, "success_perfect_5th", test_success_perfect_5th))
{
goto cleanup;
}
if (!CU_add_test (suite, "test_empty_file", test_empty_file))
{
goto cleanup;
}
if (!CU_add_test (suite, "test_too_many_notes", test_too_many_notes))
{
goto cleanup;
}
if (!CU_add_test (suite, "no_notes", test_no_notes))
{
goto cleanup;
}
if (!CU_add_test (suite, "unmatching_notes", test_unmatching_notes))
{
goto cleanup;
}
if (!CU_add_test (suite, "test_get_2_byte_octave_midi_message",
test_get_2_byte_octave_midi_message))
{
goto cleanup;
}
if (!CU_add_test (suite, "test_get_bulk_tuning_midi_message",
test_get_bulk_tuning_midi_message))
{
goto cleanup;
}
if (!CU_add_test (suite, "test_get_2_byte_octave_midi_message_test",
test_get_2_byte_octave_midi_message_test))
{
goto cleanup;
}
if (!CU_add_test (suite, "test_get_bulk_tuning_midi_message_tet",
test_get_bulk_tuning_midi_message_tet))
{
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 ();
}
|