From c7896df42057dd883ba800d6081568efc74c344a Mon Sep 17 00:00:00 2001 From: "brykt@google.com" Date: Fri, 30 Nov 2012 12:37:14 +0000 Subject: [PATCH] Fixed bug that caused frame_cutter_unittest to fail when built with MVS2008. This was caused by not supplying a correct pointer to where fread should read. The files are now opened in binary mode (which I have under stood can cause problems between different OS if it is not done). I also check for EOF when I compare data from fread. Previously the checking for correct amount of bytes read failed when the end of the file had been reached. BUG= Review URL: https://webrtc-codereview.appspot.com/937032 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3212 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/tools/frame_cutter/frame_cutter_lib.cc | 4 +-- .../frame_cutter/frame_cutter_unittest.cc | 31 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/webrtc/tools/frame_cutter/frame_cutter_lib.cc b/webrtc/tools/frame_cutter/frame_cutter_lib.cc index c387cd4bc..33597bf3c 100644 --- a/webrtc/tools/frame_cutter/frame_cutter_lib.cc +++ b/webrtc/tools/frame_cutter/frame_cutter_lib.cc @@ -29,7 +29,7 @@ int FrameCutter(const string& in_path, int width, int height, return -10; } - FILE* in_fid = fopen(in_path.c_str() , "r"); + FILE* in_fid = fopen(in_path.c_str() , "rb"); if (!in_fid) { fprintf(stderr, "Could not read input file: %s.\n", in_path.c_str()); return -11; @@ -40,7 +40,7 @@ int FrameCutter(const string& in_path, int width, int height, webrtc::scoped_array temp_buffer(new uint8_t[frame_length]); - FILE* out_fid = fopen(out_path.c_str(), "w"); + FILE* out_fid = fopen(out_path.c_str(), "wb"); if (!out_fid) { fprintf(stderr, "Could not open output file: %s.\n", out_path.c_str()); diff --git a/webrtc/tools/frame_cutter/frame_cutter_unittest.cc b/webrtc/tools/frame_cutter/frame_cutter_unittest.cc index a58cc3b98..ad2b588c0 100644 --- a/webrtc/tools/frame_cutter/frame_cutter_unittest.cc +++ b/webrtc/tools/frame_cutter/frame_cutter_unittest.cc @@ -44,9 +44,9 @@ TEST(FrameCutterUnittest, ValidInPath) { last_frame_to_cut, test_video); EXPECT_EQ(0, result); - FILE* ref_video_fid = fopen(ref_video.c_str(), "r"); + FILE* ref_video_fid = fopen(ref_video.c_str(), "rb"); ASSERT_TRUE(ref_video_fid != NULL); - FILE* test_video_fid = fopen(test_video.c_str(), "r"); + FILE* test_video_fid = fopen(test_video.c_str(), "rb"); ASSERT_TRUE(test_video_fid != NULL); const int frame_size =CalcBufferSize(kI420, width, height); @@ -55,30 +55,33 @@ TEST(FrameCutterUnittest, ValidInPath) { scoped_array test_buffer(new int[frame_size]); for (int i = 0; i < first_frame_to_cut; ++i) { - num_bytes_read = fread(ref_buffer.get(), frame_size, 1, ref_video_fid); + num_bytes_read = fread(ref_buffer.get(), 1, frame_size, ref_video_fid); EXPECT_EQ(frame_size, num_bytes_read); - num_bytes_read = fread(test_buffer.get(), frame_size, 1, test_video_fid); + num_bytes_read = fread(test_buffer.get(), 1, frame_size, test_video_fid); EXPECT_EQ(frame_size, num_bytes_read); EXPECT_EQ(0, memcmp(ref_buffer.get(), test_buffer.get(), frame_size)); } // Do not compare the frames that have been cut. for (int i = first_frame_to_cut; i <= last_frame_to_cut; ++i) { - num_bytes_read = fread(&ref_buffer, frame_size, 1, ref_video_fid); + num_bytes_read = fread(ref_buffer.get(), 1, frame_size, ref_video_fid); EXPECT_EQ(frame_size, num_bytes_read); } - while (!feof(test_video_fid)) { - num_bytes_read = fread(&ref_buffer, frame_size, 1, ref_video_fid); - EXPECT_EQ(frame_size, num_bytes_read); - num_bytes_read = fread(&test_buffer, frame_size, 1, test_video_fid); - EXPECT_EQ(frame_size, num_bytes_read); - EXPECT_EQ(0, memcmp(ref_buffer.get(), test_buffer.get(), frame_size)); + while (!feof(test_video_fid) && !feof(ref_video_fid)) { + num_bytes_read = fread(ref_buffer.get(), 1, frame_size, ref_video_fid); + if (!feof(ref_video_fid)) { + EXPECT_EQ(frame_size, num_bytes_read); + } + num_bytes_read = fread(test_buffer.get(), 1, frame_size, test_video_fid); + if (!feof(test_video_fid)) { + EXPECT_EQ(frame_size, num_bytes_read); + } + if (!feof(test_video_fid) && !feof(test_video_fid)) { + EXPECT_EQ(0, memcmp(ref_buffer.get(), test_buffer.get(), frame_size)); + } } - bool are_both_files_at_the_end = - (feof(test_video_fid) && feof(test_video_fid)); - EXPECT_TRUE(are_both_files_at_the_end); } TEST(FrameCutterUnittest, EmptySetToCut) {