00001 #include "../FCam.h"
00002 #include <stdio.h>
00003
00004 using namespace FCam;
00005
00006 int main(int argc, const char **argv) {
00007 printf("Testing the image class\n");
00008
00009 printf("Constructing some images\n");
00010 Image small(640, 480, UYVY);
00011 small.debug();
00012
00013 Image big(1024, 1024, UYVY);
00014 big.debug();
00015
00016 unsigned char data[100*100*2];
00017 Image weak(100, 100, UYVY, &data[0]);
00018 weak.debug();
00019
00020 {
00021 printf("\nTesting copy constructor\n");
00022 Image newsmall(small);
00023 small.debug();
00024 newsmall.debug();
00025
00026 printf("\n");
00027 Image newweak(weak);
00028 weak.debug();
00029 newweak.debug();
00030 printf("Destroying copies...\n");
00031 }
00032 small.debug();
00033 weak.debug();
00034
00035 {
00036 printf("\nTesting assignment operator\n");
00037
00038 Image newsmall = small;
00039 small.debug();
00040 newsmall.debug();
00041
00042 Image newweak = weak;
00043 weak.debug();
00044 newweak.debug();
00045
00046 printf("Destroying assigned copies...\n");
00047 }
00048 small.debug();
00049 weak.debug();
00050
00051 {
00052 printf("\nTesting locking a copy\n");
00053 Image newsmall(small);
00054 Image newweak(weak);
00055
00056 newsmall.lock();
00057 if (small.lock(0)) {
00058 printf("ERROR: should not have been able to acquire lock on original when copy is locked\n");
00059 small.debug();
00060 newsmall.debug();
00061 return -1;
00062 }
00063 newsmall.unlock();
00064
00065 if (!small.lock(0)) {
00066 printf("ERROR: should be able to acquire lock on original when copy is unlocked\n");
00067 small.debug();
00068 newsmall.debug();
00069 return -1;
00070 }
00071
00072 if (newsmall.lock(0)) {
00073 printf("ERROR: should be not able to acquire lock on copy where original is locked\n");
00074 small.debug();
00075 newsmall.debug();
00076 return -1;
00077 }
00078
00079 printf("\nTesting destroying an unlocked copy while original is locked\n");
00080 }
00081
00082 weak.unlock();
00083 small.unlock();
00084 small.debug();
00085 weak.debug();
00086
00087 {
00088 printf("Testing making a copy, then setting the copies to special values\n");
00089
00090 Image newsmall(small);
00091 newsmall.setData(Image::AutoAllocate);
00092 small.debug();
00093 newsmall.debug();
00094
00095 Image newweak(weak);
00096 newweak.setData(Image::Discard);
00097 weak.debug();
00098 newweak.debug();
00099
00100 printf("\nTesting deleting images with special values\n");
00101 }
00102 small.debug();
00103 weak.debug();
00104
00105 {
00106 printf("\nTesting assigning myself to a copy\n");
00107 Image foo(small);
00108 small = foo;
00109 small.debug();
00110 }
00111 small.debug();
00112
00113 {
00114 printf("\nTesting decref of assigned copies\n");
00115 for (int i = 0; i < 10; i++) {
00116 Image newsmall(small);
00117 newsmall = weak;
00118 }
00119 }
00120 small.debug();
00121 weak.debug();
00122
00123
00124 {
00125 printf("\nTesting assigning myself to myself\n");
00126 small = small;
00127 }
00128 small.debug();
00129
00130 {
00131 printf("\nTesting making a backup, nuking myself, and restoring from backup\n");
00132 Image newsmall(small);
00133 small.setData(Image::Discard);
00134 small = newsmall;
00135
00136 Image newweak(weak);
00137 weak.setData(Image::Discard);
00138 weak = newweak;
00139 }
00140 small.debug();
00141 weak.debug();
00142
00143 printf("\nTiming creation and deletion of lots of images\n");
00144 Time start = Time::now();
00145 for (int i = 0; i < 1000; i++) {
00146 Image *a = new Image(640, 480, UYVY);
00147 Image *b = new Image(640, 480, UYVY);
00148 Image *c = new Image(640, 480, UYVY);
00149 Image *d = new Image(640, 480, UYVY);
00150 Image *e = new Image(640, 480, UYVY);
00151 delete e;
00152 delete d;
00153 delete c;
00154 delete b;
00155 delete a;
00156 }
00157 Time end = Time::now();
00158 printf("Took %d us per allocation and deletion\n", (end-start)/5000);
00159
00160 printf("\nTesting image locking\n");
00161 {
00162 Image newsmall(small);
00163 small.lock();
00164 printf("This should not deadlock...\n");
00165 start = Time::now();
00166 if (newsmall.lock(10000)) {
00167 printf("ERROR: two different references to an image both successfully locked it\n");
00168 return 1;
00169 }
00170 end = Time::now();
00171 printf("Timeout on locking an image took %d us when it should take %d us\n", end-start, 10000);
00172 printf("Unlocking image\n");
00173 small.unlock();
00174 printf("Locking different reference and letting it fall out of scope\n");
00175 newsmall.lock();
00176 }
00177 printf("Relocking image\n");
00178 small.lock();
00179 small.unlock();
00180 printf("Success!\n");
00181
00182 return 0;
00183 }