corona
1.0.2
|
00001 #include <stdio.h> 00002 #include "FileTests.h" 00003 00004 00005 inline int GetFileSize(File* file) { 00006 int pos = file->tell(); 00007 if (!file->seek(0, File::END)) { 00008 return -1; 00009 } 00010 int end = file->tell(); 00011 if (!file->seek(pos, File::BEGIN)) { 00012 return -1; 00013 } 00014 return end; 00015 } 00016 00017 00018 void 00019 FileTests::testMemoryFiles() { 00020 // a valid size but no data? 00021 std::auto_ptr<File> invalid_file1(CreateMemoryFile(0, 1)); 00022 CPPUNIT_ASSERT(invalid_file1.get() == 0); 00023 00024 // invalid size 00025 char dummy[3]; 00026 std::auto_ptr<File> invalid_file2(CreateMemoryFile(dummy, -1)); 00027 CPPUNIT_ASSERT(invalid_file2.get() == 0); 00028 00029 // no size, no buffer 00030 std::auto_ptr<File> empty_file(CreateMemoryFile(0, 0)); 00031 CPPUNIT_ASSERT(empty_file.get() != 0); 00032 00033 dummy[0] = 1; 00034 dummy[1] = 2; 00035 dummy[2] = 3; 00036 CPPUNIT_ASSERT(empty_file->write(dummy, 3) == 3); 00037 CPPUNIT_ASSERT(GetFileSize(empty_file.get()) == 3); 00038 CPPUNIT_ASSERT(empty_file->read(dummy, 3) == 0); 00039 CPPUNIT_ASSERT(empty_file->seek(0, File::BEGIN) == true); 00040 CPPUNIT_ASSERT(empty_file->read(dummy, 3) == 3); 00041 CPPUNIT_ASSERT(dummy[0] == 1 && dummy[1] == 2 && dummy[2] == 3); 00042 00043 byte* buffer = new byte[101]; 00044 for (int i = 0; i < 101; ++i) { 00045 buffer[i] = i; 00046 } 00047 auto_ptr<File> file(CreateMemoryFile(buffer, 101)); 00048 delete[] buffer; 00049 00050 // make sure the file is valid 00051 CPPUNIT_ASSERT(file.get() != 0); 00052 00053 char beginning[10]; 00054 char end[20]; 00055 CPPUNIT_ASSERT(file->read(beginning, 10) == 10); 00056 CPPUNIT_ASSERT(file->seek(-10, File::END)); 00057 CPPUNIT_ASSERT(file->read(end, 20) == 10); 00058 00059 for (int i = 0; i < 10; ++i) { 00060 CPPUNIT_ASSERT(beginning[i] == i); 00061 CPPUNIT_ASSERT(end[i] == i + 91); 00062 } 00063 } 00064 00065 00066 void 00067 FileTests::testMemoryLoads() { 00068 static string images[] = { 00069 "bmpsuite/g04rle.bmp", 00070 "gif/solid2.gif", 00071 "jpeg/63-restart.jpeg", 00072 "pcx/test.pcx", 00073 "pngsuite/g07n3p04.png", 00074 "targa/test.tga", 00075 }; 00076 static const int image_count = sizeof(images) / sizeof(*images); 00077 00078 for (int i = 0; i < image_count; ++i) { 00079 string filename = "images/" + images[i]; 00080 00081 // first, load the file into a big block of memory 00082 FILE* file = fopen(filename.c_str(), "rb"); 00083 CPPUNIT_ASSERT(file != 0); 00084 fseek(file, 0, SEEK_END); 00085 int file_size = ftell(file); 00086 byte* buffer = new byte[file_size]; 00087 fseek(file, 0, SEEK_SET); 00088 CPPUNIT_ASSERT((int)fread(buffer, 1, file_size, file) == file_size); 00089 fclose(file); 00090 00091 // then create a memory file from it 00092 auto_ptr<File> memfile(CreateMemoryFile(buffer, file_size)); 00093 CPPUNIT_ASSERT(memfile.get() != 0); 00094 delete[] buffer; 00095 00096 // load a copy of the image from the memfile 00097 auto_ptr<Image> memimage(OpenImage(memfile.get())); 00098 CPPUNIT_ASSERT_MESSAGE("opening " + images[i], memimage.get() != 0); 00099 00100 // then ask Corona to load it without using a MemFile 00101 auto_ptr<Image> diskimage(OpenImage(filename.c_str())); 00102 CPPUNIT_ASSERT_MESSAGE("opening " + images[i], diskimage.get() != 0); 00103 00104 AssertImagesEqual(images[i], memimage.get(), diskimage.get()); 00105 } 00106 } 00107 00108 00109 Test* 00110 FileTests::suite() { 00111 typedef TestCaller<FileTests> Caller; 00112 00113 TestSuite* suite = new TestSuite(); 00114 suite->addTest(new Caller("MemoryFile Tests", &FileTests::testMemoryFiles)); 00115 suite->addTest(new Caller("Load from MemoryFile", &FileTests::testMemoryLoads)); 00116 return suite; 00117 }