소스 검색

Fixed bug in ImageFile::name2Format, added unit test

Clemens-A. Brust 11 년 전
부모
커밋
e39f178ffa
3개의 변경된 파일15개의 추가작업 그리고 0개의 파일을 삭제
  1. 3 0
      core/image/ImageFile.cpp
  2. 6 0
      core/image/tests/TestImageFile.cpp
  3. 6 0
      core/image/tests/TestImageFile.h

+ 3 - 0
core/image/ImageFile.cpp

@@ -240,6 +240,9 @@ ImageFile::Format ImageFile::name2Format(const std::string &filename)
 	#endif
 
 		int pos=filename.find_last_of('.');
+		if(pos==std::string::npos)
+			return ImageFile::FormatUnknown;
+
 		magic=filename.substr(pos);
 		if(magic==".pgm" || magic==".PGM")
 				return ImageFile::PGM_RAW;

+ 6 - 0
core/image/tests/TestImageFile.cpp

@@ -295,6 +295,12 @@ void TestImageFile::testJPG_IO()
     }
 }
 
+void TestImageFile::testInvalidFileName() {
+	ImageFile image_file;
+	ImageFile::Format fileformat = image_file.name2Format("nodothere");
+	CPPUNIT_ASSERT_EQUAL(fileformat,ImageFile::FormatUnknown);
+}
+
 #endif
 
 

+ 6 - 0
core/image/tests/TestImageFile.h

@@ -21,6 +21,7 @@ class TestImageFile : public CppUnit::TestFixture
 	CPPUNIT_TEST( testColorImage );
 	CPPUNIT_TEST( testGrayImage  );
 	CPPUNIT_TEST( testJPG_IO     );
+	CPPUNIT_TEST( testInvalidFileName );
 	CPPUNIT_TEST_SUITE_END();
 
    private:
@@ -51,6 +52,11 @@ class TestImageFile : public CppUnit::TestFixture
 	* Test JPEG Input and Output
 	*/
   	void testJPG_IO();
+
+  	/**
+  	* Test for correct detection of file names without appropriate endings
+  	*/
+  	void testInvalidFileName();
 };
 
 #endif // _TESTIMAGEFILE_IMAGE_H