📄 write_read_test.tpl
字号:
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_[+ (get "data_type") +]_or_die (file, pass, test, DATA_LENGTH, __LINE__) ; /* Check the data. */ for (k = 0 ; k < DATA_LENGTH ; k++) if ([+ (get "error_func") +] (orig [k], test [k])) { printf ("\n\nLine %d (pass %d): Error at sample %d ([+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, pass, k, orig [k], test [k]) ; oct_save_[+ (get "data_type") +] (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, [+ (get "max_val") +]) ; orig = ([+ (get "data_type") +]*) orig_data ; test = ([+ (get "data_type") +]*) 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_[+ (get "data_type") +]_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 ([+ (get "data_type") +])) ; 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_[+ (get "data_type") +]_or_die (file, 0, test, frames, __LINE__) ; for (k = 0 ; k < items ; k++) if ([+ (get "error_func") +] (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\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_[+ (get "data_type") +]_or_die (file, 0, test, 2, __LINE__) ; for (k = 0 ; k < 4 ; k++) if ([+ (get "error_func") +] (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\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_[+ (get "data_type") +] (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_[+ (get "data_type") +]_or_die (file, 0, test + 10, 2, __LINE__) ; for (k = 20 ; k < 24 ; k++) if ([+ (get "error_func") +] (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\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_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ; for (k = 40 ; k < 44 ; k++) if ([+ (get "error_func") +] (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\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_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ; for (k = 20 ; k < 24 ; k++) if ([+ (get "error_func") +] (test [k], orig [k])) { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\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_[+ (get "type_name") +] */[+ ENDFOR data_type +]/*----------------------------------------------------------------------------------------*/static voidempty_file_test (const char *filename, int format){ SNDFILE *file ; SF_INFO info ; int allow_fd ; /* Sd2 files cannot be opened from an existing file descriptor. */ allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ; print_test_name ("empty_file_test", filename) ; unlink (filename) ; info.samplerate = 48000 ; info.channels = 2 ; info.format = format ; if (sf_format_check (&info) == SF_FALSE) { info.channels = 1 ; if (sf_format_check (&info) == SF_FALSE) { puts ("invalid file format") ; return ; } ; } ; /* Create an empty file. */ file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ; sf_close (file) ; /* Open for read and check the length. */ file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ; if (SF_COUNT_TO_LONG (info.frames) != 0) { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ; exit (1) ; } ; sf_close (file) ; /* Open for read/write and check the length. */ file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ; if (SF_COUNT_TO_LONG (info.frames) != 0) { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ; exit (1) ; } ; sf_close (file) ; /* Open for read and check the length. */ file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ; if (SF_COUNT_TO_LONG (info.frames) != 0) { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ; exit (1) ; } ; sf_close (file) ; check_open_file_count_or_die (__LINE__) ; unlink (filename) ; puts ("ok") ; return ;} /* empty_file_test *//*----------------------------------------------------------------------------------------*/static voidcreate_short_file (const char *filename){ FILE *file ; if (! (file = fopen (filename, "w"))) { printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ; fflush (stdout) ; perror (NULL) ; exit (1) ; } ; fprintf (file, "This is the file data.\n") ; fclose (file) ;} /* create_short_file */#if (defined (WIN32) || defined (__WIN32))/* Win32 does not have truncate (nor does it have the POSIX function ftruncate).** Hack somethng up here to over come this. This function can only truncate to a** length of zero.*/static inttruncate (const char *filename, int ignored){ int fd ; ignored = 0 ; if ((fd = open (filename, O_RDWR | O_TRUNC | O_BINARY)) < 0) return 0 ; close (fd) ; return 0 ;} /* truncate */#endif[+ COMMENT Do not edit or modify anything in this comment block. The following line is a file identity tag for the GNU Arch revision control system. arch-tag: 4187de93-d434-41a2-93a9-4f6e2995b5c1+]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -