📄 write_read_test.c
字号:
test_write_short_or_die (file, 0, orig, items, __LINE__) ; /* Add non-audio data after the audio. */ sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ; sf_close (file) ; memset (test, 0, items * sizeof (short)) ; if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ; if (sfinfo.format != format) { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames < items) { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ; exit (1) ; } ; if (! long_file_ok && sfinfo.frames > items) { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; test_read_short_or_die (file, 0, test, items, __LINE__) ; for (k = 0 ; k < items ; k++) if (CHAR_ERROR (orig [k], test [k])) { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ; exit (1) ; } ; /* Seek to start of file. */ test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; test_read_short_or_die (file, 0, test, 4, __LINE__) ; for (k = 0 ; k < 4 ; k++) if (CHAR_ERROR (orig [k], test [k])) { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ; exit (1) ; } ; if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 || (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24) { sf_close (file) ; unlink (filename) ; printf ("ok\n") ; return ; } ; /* Seek to offset from start of file. */ test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ; for (k = 10 ; k < 14 ; k++) if (CHAR_ERROR (orig [k], test [k])) { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ; exit (1) ; } ; /* Seek to offset from current position. */ test_seek_or_die (file, 6, SEEK_CUR, 20, sfinfo.channels, __LINE__) ; test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ; for (k = 20 ; k < 24 ; k++) if (CHAR_ERROR (orig [k], test [k])) { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ; exit (1) ; } ; /* Seek to offset from end of file. */ test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ; test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ; for (k = 10 ; k < 14 ; k++) if (CHAR_ERROR (orig [k], test [k])) { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ; exit (1) ; } ; /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */ test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; count = 0 ; while (count < sfinfo.frames) count += sf_read_short (file, test, 311) ; /* Check that no error has occurred. */ if (sf_error (file)) { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ; puts (sf_strerror (file)) ; exit (1) ; } ; /* Check that we haven't read beyond EOF. */ if (count > sfinfo.frames) { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ; exit (1) ; } ; test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ; sf_close (file) ; /*================================================================================== ** Now test Mono RDWR. */ sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = DATA_LENGTH ; sfinfo.channels = 1 ; sfinfo.format = format ; if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) unlink (filename) ; else { /* Create a short file. */ create_short_file (filename) ; /* Opening a already existing short file (ie invalid header) RDWR is disallowed. ** If this returns a valif pointer sf_open() screwed up. */ if ((file = sf_open (filename, SFM_RDWR, &sfinfo))) { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ; exit (1) ; } ; /* Truncate the file to zero bytes. */ if (truncate (filename, 0) < 0) { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ; perror (NULL) ; exit (1) ; } ; } ; /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain ** all the usual data required when opening the file in WRITE mode. */ sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = DATA_LENGTH ; sfinfo.channels = 1 ; sfinfo.format = format ; file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ; /* Do 3 writes followed by reads. After each, check the data and the current ** read and write offsets. */ for (pass = 1 ; pass <= 3 ; pass ++) { orig [20] = pass * 2 ; /* Write some data. */ test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ; test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ; /* Read what we just wrote. */ test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ; /* Check the data. */ for (k = 0 ; k < DATA_LENGTH ; k++) if (CHAR_ERROR (orig [k], test [k])) { printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ; oct_save_short (orig, test, DATA_LENGTH) ; exit (1) ; } ; test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ; } ; /* for (pass ...) */ sf_close (file) ; /* Open the file again to check the data. */ file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ; if (sfinfo.format != format) { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames < 3 * DATA_LENGTH) { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ; exit (1) ; } if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH) { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; if (! long_file_ok) test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ; else test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ; for (pass = 1 ; pass <= 3 ; pass ++) { orig [20] = pass * 2 ; test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ; /* Read what we just wrote. */ test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ; /* Check the data. */ for (k = 0 ; k < DATA_LENGTH ; k++) if (CHAR_ERROR (orig [k], test [k])) { printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ; oct_save_short (orig, test, DATA_LENGTH) ; exit (1) ; } ; } ; /* for (pass ...) */ sf_close (file) ; /*================================================================================== ** Now test Stereo. */ sfinfo.samplerate = 44100 ; sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */ sfinfo.channels = 2 ; sfinfo.format = format ; if (! sf_format_check (&sfinfo)) { unlink (filename) ; printf ("ok, (no stereo)\n") ; return ; } ; gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ; orig = (short*) orig_data ; test = (short*) test_data ; /* Make this a macro so gdb steps over it in one go. */ CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ; items = DATA_LENGTH ; frames = items / sfinfo.channels ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ; sf_set_string (file, SF_STR_ARTIST, "Your name here") ; test_writef_short_or_die (file, 0, orig, frames, __LINE__) ; sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ; sf_close (file) ; memset (test, 0, items * sizeof (short)) ; if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ; if (sfinfo.format != format) { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames < frames) { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ; exit (1) ; } ; if (! long_file_ok && sfinfo.frames > frames) { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ; exit (1) ; } ; if (sfinfo.channels != 2) { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; test_readf_short_or_die (file, 0, test, frames, __LINE__) ; for (k = 0 ; k < items ; k++) if (CHAR_ERROR (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ; exit (1) ; } ; /* Seek to start of file. */ test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; test_readf_short_or_die (file, 0, test, 2, __LINE__) ; for (k = 0 ; k < 4 ; k++) if (CHAR_ERROR (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ; exit (1) ; } ; /* Seek to offset from start of file. */ test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; /* Check for errors here. */ if (sf_error (file)) { printf ("Line %d: Should NOT return an error.\n", __LINE__) ; puts (sf_strerror (file)) ; exit (1) ; } ; if (sf_read_short (file, test, 1) > 0) { printf ("Line %d: Should return 0.\n", __LINE__) ; exit (1) ; } ; if (! sf_error (file)) { printf ("Line %d: Should return an error.\n", __LINE__) ; exit (1) ; } ; /*-----------------------*/ test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ; for (k = 20 ; k < 24 ; k++) if (CHAR_ERROR (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ; exit (1) ; } ; /* Seek to offset from current position. */ test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ; test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ; for (k = 40 ; k < 44 ; k++) if (CHAR_ERROR (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ; exit (1) ; } ; /* Seek to offset from end of file. */ test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ; test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ; for (k = 20 ; k < 24 ; k++) if (CHAR_ERROR (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ; exit (1) ; } ; sf_close (file) ; delete_file (format, filename) ; check_open_file_count_or_die (__LINE__) ; puts ("ok") ; return ;} /* pcm_test_char */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -