corona
1.0.2
|
00001 #include <algorithm> 00002 #include "APITests.h" 00003 00004 00005 void 00006 APITests::testBasicOperations(int width, int height) { 00007 const PixelFormat format = PF_R8G8B8A8; 00008 const int bpp = 4; 00009 00010 auto_ptr<Image> image(CreateImage(width, height, format)); 00011 CPPUNIT_ASSERT(image->getWidth() == width); 00012 CPPUNIT_ASSERT(image->getHeight() == height); 00013 CPPUNIT_ASSERT(image->getFormat() == format); 00014 00015 // verify that the image is black 00016 byte* pixels = (byte*)image->getPixels(); 00017 for (int i = 0; i < width * height * bpp; ++i) { 00018 CPPUNIT_ASSERT(pixels[i] == 0); 00019 } 00020 00021 // fill the image with random pixels 00022 for (int i = 0; i < width * height * bpp; ++i) { 00023 pixels[i] = rand() % 256; 00024 } 00025 00026 auto_ptr<Image> create_clone( 00027 CreateImage(image->getWidth(), image->getHeight(), 00028 image->getFormat(), image->getPixels())); 00029 CPPUNIT_ASSERT(create_clone.get() != 0); 00030 CPPUNIT_ASSERT(image->getWidth() == create_clone->getWidth()); 00031 CPPUNIT_ASSERT(image->getHeight() == create_clone->getHeight()); 00032 CPPUNIT_ASSERT(image->getFormat() == create_clone->getFormat()); 00033 CPPUNIT_ASSERT(memcmp(image->getPixels(), 00034 create_clone->getPixels(), 00035 width * height * bpp) == 0); 00036 00037 // clone the image (use same pixel format) 00038 auto_ptr<Image> identical_clone(CloneImage(image.get())); 00039 CPPUNIT_ASSERT(image->getWidth() == identical_clone->getWidth()); 00040 CPPUNIT_ASSERT(image->getHeight() == identical_clone->getHeight()); 00041 CPPUNIT_ASSERT(image->getFormat() == identical_clone->getFormat()); 00042 CPPUNIT_ASSERT(memcmp(image->getPixels(), 00043 identical_clone->getPixels(), 00044 width * height * bpp) == 0); 00045 00046 // clone the image, removing the alpha channel 00047 auto_ptr<Image> other_clone(CloneImage(identical_clone.get(), PF_R8G8B8)); 00048 CPPUNIT_ASSERT(image->getWidth() == other_clone->getWidth()); 00049 CPPUNIT_ASSERT(image->getHeight() == other_clone->getHeight()); 00050 CPPUNIT_ASSERT(other_clone->getFormat() == PF_R8G8B8); 00051 byte* image_p = (byte*)image->getPixels(); 00052 byte* other_p = (byte*)other_clone->getPixels(); 00053 for (int i = 0; i < width * height; ++i) { 00054 CPPUNIT_ASSERT(*image_p++ == *other_p++); 00055 CPPUNIT_ASSERT(*image_p++ == *other_p++); 00056 CPPUNIT_ASSERT(*image_p++ == *other_p++); 00057 ++image_p; // skip alpha 00058 } 00059 00060 // flip the image 00061 // clone source first, since flip frees the original 00062 auto_ptr<Image> flip_none(FlipImage(CloneImage(image.get()), 0)); 00063 auto_ptr<Image> flip_x (FlipImage(CloneImage(image.get()), CA_X)); 00064 auto_ptr<Image> flip_y (FlipImage(CloneImage(image.get()), CA_Y)); 00065 auto_ptr<Image> flip_xy (FlipImage(CloneImage(image.get()), CA_X | CA_Y)); 00066 00067 AssertImagesEqual("No flipping", flip_none.get(), image.get()); 00068 00069 CPPUNIT_ASSERT(flip_x.get() != 0); 00070 CPPUNIT_ASSERT(width == flip_x->getWidth()); 00071 CPPUNIT_ASSERT(height == flip_x->getHeight()); 00072 CPPUNIT_ASSERT(format == flip_x->getFormat()); 00073 00074 CPPUNIT_ASSERT(flip_y.get() != 0); 00075 CPPUNIT_ASSERT(width == flip_y->getWidth()); 00076 CPPUNIT_ASSERT(height == flip_y->getHeight()); 00077 CPPUNIT_ASSERT(format == flip_y->getFormat()); 00078 00079 CPPUNIT_ASSERT(flip_xy.get() != 0); 00080 CPPUNIT_ASSERT(width == flip_xy->getWidth()); 00081 CPPUNIT_ASSERT(height == flip_xy->getHeight()); 00082 CPPUNIT_ASSERT(format == flip_xy->getFormat()); 00083 00084 const byte* flip_x_pixels = (const byte*)flip_x->getPixels(); 00085 const byte* flip_y_pixels = (const byte*)flip_y->getPixels(); 00086 const byte* flip_xy_pixels = (const byte*)flip_xy->getPixels(); 00087 00088 for (int h = 0; h < height; h++) { 00089 for (int w = 0; w < width; w++) { 00090 const int image_index = (h * width + w) * bpp; 00091 const int opp_w = width - 1 - w; 00092 const int opp_h = height - 1 - h; 00093 const int flip_x_index = (opp_h * width + w) * bpp; 00094 const int flip_y_index = (h * width + opp_w) * bpp; 00095 const int flip_xy_index = (opp_h * width + opp_w) * bpp; 00096 00097 for (int p = 0; p < bpp; p++) { 00098 CPPUNIT_ASSERT(pixels[image_index] == flip_x_pixels [flip_x_index]); 00099 CPPUNIT_ASSERT(pixels[image_index] == flip_y_pixels [flip_y_index]); 00100 CPPUNIT_ASSERT(pixels[image_index] == flip_xy_pixels[flip_xy_index]); 00101 } 00102 } 00103 } 00104 00105 } 00106 00107 00108 void 00109 APITests::testAPI() { 00110 for (int w = 0; w <= 16; ++w) { 00111 for (int h = 0; h <= 16; ++h) { 00112 testBasicOperations(w, h); 00113 } 00114 } 00115 } 00116 00117 00120 class TestDeletionImage : public DLLImplementation<Image> { 00121 public: 00122 TestDeletionImage( 00123 int width, int height, 00124 PixelFormat format, byte* pixels, 00125 byte* palette, int palette_size, PixelFormat palette_format, 00126 bool& deleted) 00127 : m_deleted(deleted) 00128 { 00129 m_width = width; 00130 m_height = height; 00131 m_format = format; 00132 m_pixels = pixels; 00133 m_palette = palette; 00134 m_palette_size = palette_size; 00135 m_palette_format = palette_format; 00136 } 00137 00138 ~TestDeletionImage() { 00139 delete[] m_pixels; 00140 delete[] m_palette; 00141 m_deleted = true; 00142 } 00143 00144 int COR_CALL getWidth() { 00145 return m_width; 00146 } 00147 00148 int COR_CALL getHeight() { 00149 return m_height; 00150 } 00151 00152 PixelFormat COR_CALL getFormat() { 00153 return m_format; 00154 } 00155 00156 void* COR_CALL getPixels() { 00157 return m_pixels; 00158 } 00159 00160 void* COR_CALL getPalette() { 00161 return m_palette; 00162 } 00163 00164 int COR_CALL getPaletteSize() { 00165 return m_palette_size; 00166 } 00167 00168 PixelFormat COR_CALL getPaletteFormat() { 00169 return m_palette_format; 00170 } 00171 00172 private: 00173 int m_width; 00174 int m_height; 00175 PixelFormat m_format; 00176 byte* m_pixels; 00177 byte* m_palette; 00178 int m_palette_size; 00179 PixelFormat m_palette_format; 00180 bool& m_deleted; 00181 }; 00182 00183 00184 void 00185 APITests::testFormatQueries() { 00186 CPPUNIT_ASSERT(GetPixelSize(PF_R8G8B8A8) == 4); 00187 CPPUNIT_ASSERT(GetPixelSize(PF_B8G8R8A8) == 4); 00188 CPPUNIT_ASSERT(GetPixelSize(PF_R8G8B8) == 3); 00189 CPPUNIT_ASSERT(GetPixelSize(PF_B8G8R8) == 3); 00190 CPPUNIT_ASSERT(GetPixelSize(PF_I8) == 1); 00191 CPPUNIT_ASSERT(GetPixelSize(PF_DONTCARE) == 0); 00192 00193 CPPUNIT_ASSERT(IsDirect(PF_R8G8B8A8)); 00194 CPPUNIT_ASSERT(IsDirect(PF_B8G8R8A8)); 00195 CPPUNIT_ASSERT(IsDirect(PF_R8G8B8)); 00196 CPPUNIT_ASSERT(IsDirect(PF_B8G8R8)); 00197 CPPUNIT_ASSERT(!IsDirect(PF_I8)); 00198 CPPUNIT_ASSERT(!IsDirect(PF_DONTCARE)); 00199 00200 CPPUNIT_ASSERT(!IsPalettized(PF_R8G8B8A8)); 00201 CPPUNIT_ASSERT(!IsPalettized(PF_B8G8R8A8)); 00202 CPPUNIT_ASSERT(!IsPalettized(PF_R8G8B8)); 00203 CPPUNIT_ASSERT(!IsPalettized(PF_B8G8R8)); 00204 CPPUNIT_ASSERT(IsPalettized(PF_I8)); 00205 CPPUNIT_ASSERT(!IsPalettized(PF_DONTCARE)); 00206 00207 CPPUNIT_ASSERT(GetPaletteSize(PF_R8G8B8A8) == 0); 00208 CPPUNIT_ASSERT(GetPaletteSize(PF_B8G8R8A8) == 0); 00209 CPPUNIT_ASSERT(GetPaletteSize(PF_R8G8B8) == 0); 00210 CPPUNIT_ASSERT(GetPaletteSize(PF_B8G8R8) == 0); 00211 CPPUNIT_ASSERT(GetPaletteSize(PF_I8) == 256); 00212 CPPUNIT_ASSERT(GetPaletteSize(PF_DONTCARE) == 0); 00213 } 00214 00215 00221 template<typename T> 00222 inline T* allocate(size_t size) { 00223 T* t = new T[size]; 00224 std::fill(t, t + size, T()); 00225 return t; 00226 } 00227 00228 00229 void 00230 APITests::testMemory() { 00231 { 00232 // ConvertImage() should delete the image if it successfully 00233 // converts. 00234 bool convert_image_deleted = false; 00235 Image* image = new TestDeletionImage( 00236 16, 16, PF_I8, allocate<byte>(256), allocate<byte>(256 * 4), 256, PF_R8G8B8, 00237 convert_image_deleted); 00238 delete ConvertImage(image, PF_R8G8B8A8); 00239 CPPUNIT_ASSERT(convert_image_deleted == true); 00240 } 00241 00242 { 00243 // ConvertImage() should also delete the image if it cannot 00244 // convert. 00245 bool convert_image_deleted = false; 00246 Image* image = new TestDeletionImage( 00247 16, 16, PF_I8, allocate<byte>(256), allocate<byte>(256 * 4), 256, PF_R8G8B8, 00248 convert_image_deleted); 00249 delete ConvertImage(image, PF_I8); 00250 CPPUNIT_ASSERT(convert_image_deleted == true); 00251 } 00252 00253 { 00254 bool convert_palette_deleted = false; 00255 Image* image = new TestDeletionImage( 00256 16, 16, PF_I8, allocate<byte>(256), allocate<byte>(256 * 4), 256, PF_R8G8B8, 00257 convert_palette_deleted); 00258 delete ConvertPalette(image, PF_R8G8B8A8); 00259 CPPUNIT_ASSERT(convert_palette_deleted == true); 00260 } 00261 00262 { 00263 bool convert_palette_deleted = false; 00264 Image* image = new TestDeletionImage( 00265 16, 16, PF_I8, allocate<byte>(256), allocate<byte>(256 * 4), 256, PF_R8G8B8, 00266 convert_palette_deleted); 00267 delete ConvertPalette(image, PF_I8); 00268 CPPUNIT_ASSERT(convert_palette_deleted == true); 00269 } 00270 00271 { 00272 bool convert_image_deleted = false; 00273 Image* image = new TestDeletionImage( 00274 16, 16, PF_I8, allocate<byte>(256), allocate<byte>(256 * 4), 256, PF_R8G8B8, 00275 convert_image_deleted); 00276 delete FlipImage(image, CA_X); 00277 CPPUNIT_ASSERT(convert_image_deleted == true); 00278 } 00279 } 00280 00281 00282 Test* 00283 APITests::suite() { 00284 typedef TestCaller<APITests> Caller; 00285 00286 TestSuite* suite = new TestSuite(); 00287 suite->addTest(new Caller("Basic API Tests", &APITests::testAPI)); 00288 suite->addTest(new Caller("Format Queries", &APITests::testFormatQueries)); 00289 suite->addTest(new Caller("Memory Management", &APITests::testMemory)); 00290 return suite; 00291 }