aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/selftest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/frontend/selftest.cpp')
-rw-r--r--src/frontend/selftest.cpp646
1 files changed, 646 insertions, 0 deletions
diff --git a/src/frontend/selftest.cpp b/src/frontend/selftest.cpp
new file mode 100644
index 0000000..2f3f65a
--- /dev/null
+++ b/src/frontend/selftest.cpp
@@ -0,0 +1,646 @@
1#include <FL/forms.H>
2hd24fs* testfs;
3
4void createimage_callback(void* y)
5{
6 cout << "." << endl;
7 Fl::add_timeout(1.0,createimage_callback,y);
8 return;
9}
10int passcount;
11int failcount;
12#define MINSECTORS 1353964
13
14//#define BITBIGGER (MINSECTORS+10000)
15/* this gives for a bit over 5 minutes at 48k, 24 tracks: */
16#define BITBIGGER (MINSECTORS+2073600)
17
18/* constant is for pretty demo image. */
19void pass()
20{
21 passcount++;
22 cout << "......................................................PASS" << endl;
23}
24void fail()
25{
26 failcount++;
27 cout << "......................................................FAIL" << endl;
28}
29void fail(const char* reason)
30{
31 failcount++;
32 cout << "......................................................FAIL" << endl;
33 cout << "Reason: "<<reason << endl;
34}
35void test_createimage()
36{
37 cout << "Test if creating too small drive images is prevented... " << endl;
38
39 __uint32 lastsector=MINSECTORS-1;
40
41 string* strtest=new string("testimage.h24");
42 char message[2048];
43 message[0]='\0';
44 Fl::add_timeout(1.0,createimage_callback,&message[0]);
45 int result=hd24utils::newdriveimage(strtest,lastsector,&message[0],NULL);
46 delete strtest;
47 Fl::remove_timeout(&createimage_callback);
48 if (result!=-1) fail(); else pass();
49 ////////////////////////////////////////////////////
50 cout << "-----------------------------------------------" << endl;
51 cout << "Create minimum size empty drive image... please wait" << endl;
52 lastsector=MINSECTORS;
53
54 strtest=new string("testimage.h24");
55 result=hd24utils::newdriveimage(strtest,lastsector,&message[0],NULL);
56
57 cout << "Image create messages=" << message << endl;
58 if (result==0) pass(); else fail();
59 // Check expected drive image contents.
60 hd24fs* testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
61 /* we calculate free space based on 1 channel because it is much
62 more sensitive than free space based on 24 channels */
63 string* freespace=testfs->freespace(48000,1);
64
65 cout << "Free space on drive: " << *freespace << endl;
66 delete testfs;
67 //////////////////////////////////////////////////////
68 cout << "-----------------------------------------------" << endl;
69 cout << "Creating somewhat larger size empty drive image... please wait" << endl;
70 lastsector=BITBIGGER;
71
72 string* strtest2=new string("testimage2.h24");
73 result=hd24utils::newdriveimage(strtest2,lastsector,&message[0],NULL);
74
75 cout << "Image create messages=" << message << endl;
76 if (result==0) pass(); else fail();
77 // Check expected drive image contents.
78 hd24fs* testfs2=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest2,false);
79 string* freespace2=testfs2->freespace(48000,1);
80 cout << "Free space on drive: " << *freespace2 << endl;
81 if (strcmp(freespace->c_str(),freespace2->c_str())==0)
82 {
83 fail("Images were supposed to have different sizes");
84 } else {
85 pass();
86 }
87 delete testfs2;
88 delete freespace2;
89 delete strtest2;
90//*/
91 delete freespace;
92 delete strtest;
93 return;
94}
95
96void test_useimage()
97{
98 char failmsg[64];
99 cout << "Try to use empty drive image... " << endl;
100 //false=force?
101 string* strtest=new string("testimage2.h24");
102 testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
103 delete strtest;
104 strtest=NULL;
105
106 // Let's see if all is as expected.
107 if (!(testfs->isOpen())) fail(); else pass();
108 string* freespace=testfs->freespace(48000,24);
109 cout << "Free space on drive: " << *freespace << endl;
110 delete freespace;
111 hd24raw* rawfs=new hd24raw(testfs);
112 /*
113 These functions are only available through raw FS
114 */
115 int songsondisk=rawfs->songsondisk();
116 cout << "Songs on disk=" << songsondisk << endl;
117 if (songsondisk!=0) fail(); else pass();
118
119 __uint32 lastsector=rawfs->getlastsectornum();
120 __uint32 expectedlastsector=BITBIGGER;
121 cout << "Last sectornum of drive image="<< lastsector << endl;
122
123
124 if (lastsector!=expectedlastsector)
125 {
126 sprintf(failmsg,"Expected last sector was %ld but found %ld",expectedlastsector,lastsector);
127 fail(failmsg);
128 }
129 else
130 {
131 pass();
132 }
133
134 string* devname=testfs->getdevicename();
135 cout <<"Device name="<<*devname<<endl;
136 if (*devname=="")
137 {
138 fail("Device name should always exist");
139 } else pass();
140
141 string* volname=testfs->volumename();
142 cout <<"Volume name="<<*volname <<endl;
143 if (*volname=="") fail("Volume name of freshly formatted drive must be nonempty"); else pass();
144
145 // set volume name, commit, then test if change OK
146 __uint32 projcount=testfs->projectcount();
147 cout <<"Project count="<<projcount << endl;
148 if (projcount!=1) fail("Newly formatted image must have exactly 1 project."); else pass();
149 hd24project* currproj=NULL;
150 if (projcount>0)
151 {
152 currproj=testfs->getproject(0); // must error
153 cout << "Check protection against getting project 0" << endl;
154 if (currproj!=NULL) fail(); else pass();
155
156 delete currproj;
157 currproj=testfs->getproject(1); // must not error
158 cout << "Check zero songcount for supposedly empty project" << endl;
159 __uint32 songcount=currproj->songcount();
160 if (songcount!=0) fail(); else pass();
161
162 cout << "Check getting nonexisting/invalid project numbers" << endl;
163 int failed=0;
164 for (int i=2; i<=100; i++)
165 {
166 currproj=testfs->getproject(i); // must not error but must return null
167 if (currproj!=NULL)
168 {
169 failed=1;
170 }
171 }
172 if (failed!=0) fail(); else pass();
173 }
174
175 cout << "set volume name in drive image" << endl;
176 testfs->setvolumename("myvol"); // only in memory!!!
177 // check if volume name has been set (in memory)
178 cout << "reading back volume name to see if set was successful" << endl;
179
180 volname=testfs->volumename();
181 if (*volname!="myvol") fail(); else pass();
182
183 cout << "set longer name in drive image" << endl;
184 testfs->setvolumename("mylongervolumename"); // only in memory!!!
185 volname=testfs->volumename();
186 if (*volname!="mylongervolumename") fail(); else pass();
187
188 cout << "Test truncating too long volume name in drive image" << endl;
189 testfs->setvolumename("myoverlylongvolumenamethemaximumissixtyfourcharactersbutwemakeitlongeranyway"); // only in memory!!!
190 volname=testfs->volumename();
191 if (strlen(volname->c_str())!=64) fail(); else pass();
192 cout << "Try to clear volume name" <<endl;
193 testfs->setvolumename("");
194 volname=testfs->volumename();
195 if (strlen(volname->c_str())!=0) fail(); else pass();
196 cout << "Setting volume name " << endl;
197 testfs->setvolumename("MyVolume");
198 cout << "and commiting for later readback." << endl;
199 testfs->savedriveinfo(); // auto-commit
200 cout << "Commit done." << endl;
201 return;
202}
203
204void test_commit()
205{
206 string* strtest=new string("testimage2.h24");
207
208 cout << "Delete testfs..." << endl;
209 delete testfs;
210 testfs=NULL;
211
212 pass();
213
214 testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
215 delete strtest;
216 cout << "Reading back volume name" << endl;
217 string* volname=testfs->volumename();
218 cout << "Read back " << *volname << endl;
219 if (*volname!="MyVolume") fail(); else pass();
220
221 // after commit, drive size must be preserved.
222 hd24raw* rawfs=new hd24raw(testfs);
223 __uint32 lastsector=rawfs->getlastsectornum();
224 __uint32 expectedlastsector=BITBIGGER;
225 cout << "Test if image has remained same size: "<< lastsector << endl;
226 if (lastsector!=expectedlastsector) fail(); else pass();
227
228
229 testfs->setvolumename("Drive Name");
230 testfs->savedriveinfo(); // commit tested before-assume it succeeds.
231}
232
233void test_project()
234{
235 string* strtest=new string("testimage2.h24");
236
237 cout << "Delete testfs..." << endl;
238 delete testfs;
239 testfs=NULL;
240
241 pass();
242
243 testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
244 delete strtest;
245 hd24raw* rawfs=new hd24raw(testfs);
246 cout << "Checking current project count... (should be 1): ";
247 int pcount=testfs->projectcount();
248 cout << pcount << endl;
249 if (pcount==1) pass(); else fail("Project count of formatted drive should be 1");
250
251 cout << "Try to get project id 0 (should not be possible)" << endl;
252 hd24project* proj=testfs->getproject(0);
253 if (proj==NULL) pass(); else
254 {
255 delete proj;
256 proj=NULL;
257 fail("Project IDs should be 1 based but are not!");
258 }
259
260 cout << "Try to get project id 1 (first and only project on drive" << endl;
261 proj=testfs->getproject(1);
262 if (proj==NULL) fail("Cannot get only project on drive"); else
263 {
264 delete proj;
265 proj=NULL;
266 pass();
267 }
268 cout << "Try to get any but the first project on the drive" << endl;
269 cout << "(using legal project numbers and one too high number)" << endl;
270 int projsgot=0;
271 int i=0;
272 for (i=2; i<=100; i++)
273 {
274 if (proj!=NULL)
275 {
276 delete proj;
277 proj=NULL;
278 }
279 proj=testfs->getproject(i);
280 if (proj!=NULL)
281 {
282 projsgot++;
283 fail("Managed to get project number:");
284 cout << i << endl;
285 delete proj;
286 proj=NULL;
287 }
288 }
289 if (proj!=NULL)
290 {
291 delete proj;
292 proj=NULL;
293 }
294
295 if (projsgot==0) pass();
296
297 cout << "Try if only project on drive is protected from deleting" << endl;
298 testfs->deleteproject(1);
299 pcount=testfs->projectcount();
300 cout << pcount << endl;
301 if (pcount==1) pass(); else fail("Only project on drive should not be deletable");
302
303 cout << "Try to create a project..." << endl;
304 hd24project* newproj=testfs->createproject("selftest project 1");
305 if (newproj==NULL)
306 {
307 fail("Creating project failed");
308 } else {
309 pass();
310 }
311 delete newproj; // we won't be using this object anymore.
312
313 cout << "Make sure project creation updates project count..." << endl;
314 pcount=testfs->projectcount();
315 if (pcount==2) pass(); else fail("Project count should have been 2 at this stage.");
316 cout << "Trying to delete newly created project..." << endl;
317 testfs->deleteproject(2);
318 pcount=testfs->projectcount();
319 if (pcount==1) pass(); else fail("Deletion successful");
320
321 cout << "Trying to create 98 new projects (should be possible)..." << endl;
322 char projname[64];
323 int failedcreate=0;
324 for (i=0;i<98;i++)
325 {
326 cout << "project=" << i << endl;
327 sprintf(projname,"selftest project %d",i+2);
328 hd24project* newproj=testfs->createproject(projname);
329 pcount=testfs->projectcount();
330 if (pcount!=(i+2))
331 {
332 sprintf(projname,"could not create project %d",i);
333 fail(projname);
334 failedcreate=1;
335 break;
336 };
337 delete newproj;
338 }
339 if (failedcreate==0)
340 {
341 pass();
342 }
343 cout << "Checking if project count is 99 as expected " << endl;
344 pcount=testfs->projectcount();
345 if (pcount==99) pass(); else fail("Nope.");
346
347 cout << "Trying if creating more projects than allowed is prevented" << endl;
348 newproj=NULL;
349 newproj=testfs->createproject(projname);
350 if (newproj!=NULL)
351 {
352 fail("Project object was returned!");
353 } else {
354 pass();
355 }
356
357 pcount=testfs->projectcount();
358 cout << "Verifying project count to still be 99" <<endl;
359 if (pcount==99) pass(); else fail("Project count unduely updated.");
360
361 cout << "Deleting last 50 projects" << endl;
362 /* this should leave 49 projects in place. */
363 for (i=1;i<60;i++)
364 {
365 testfs->deleteproject(50);
366 }
367 pcount=testfs->projectcount();
368 if (pcount==49) pass(); else fail("Incorrect project count.");
369 cout << "Creating 50th project again" << i << endl;
370 sprintf(projname,"selftest project %d",i+2);
371 if (newproj!=NULL) delete newproj;
372
373 newproj=testfs->createproject(projname);
374 pcount=testfs->projectcount();
375 cout << "Expecting project count to be 50 now..." << endl;
376 if (pcount!=50) fail("Incorrect project count"); else pass();
377
378 cout << "Checking project sector of first project (must be 0x14 or 20)" << endl;
379 __uint32 projsec= rawfs->getprojectsectornum(1);
380 cout << "Proj sector=" << projsec << endl;
381 if (projsec!=0x14) fail("Incorrect sector"); else pass();
382
383 cout << "Deleting first project (to empty a slot)" << endl;
384 pcount=testfs->projectcount();
385 cout << "project count BEFORE=" <<pcount <<endl;
386
387 testfs->deleteproject(1);
388 int pcount2=testfs->projectcount();
389 cout << "Project count AFTER=" <<pcount2 << endl;
390 if (pcount2!=(pcount-1)) fail("Expected project count to be 49."); else pass();
391 projsec= rawfs->getprojectsectornum(1);
392 cout << "Second project should originally have been on 0x15,"<<endl;
393 cout << "expecting first project to start on that sector now..." <<endl;
394 cout << "(in reality it is on " << projsec << ")" << endl;
395 if (projsec!=0x15) fail("Incorrect sector"); else pass();
396 /* now create a new project-- this project must be on sector 0x14
397 again */
398 cout << "Creating new project - it should overwrite the first project block" << endl;
399 newproj=testfs->createproject(projname);
400 pcount=testfs->projectcount();
401 cout << "Expecting project count of 50 again, in reality it is " << pcount << endl;
402 if (pcount!=50) fail("Incorrect project count"); else pass();
403 __sint32 projid=newproj->projectid();
404 cout << "Project id=" << projid << endl;
405 if (projid!=50) {
406 fail("Unexpected project id");
407 }
408 projsec=rawfs->getprojectsectornum(projid);
409 cout << "Expecting project sector of project " << projid << " to be 0x14," ;
410 cout << " in reality it is " << projsec << endl;
411
412 if (projsec!=0x14) fail("Incorrect sector"); else pass();
413
414 newproj->projectname("Changed Name");
415 string* projcname=newproj->projectname();
416 if (*projcname!="Changed Name") fail("Project name change unsuccessful"); else pass();
417 delete projcname;
418 newproj->save();
419 delete rawfs;
420}
421
422
423void test_proj2()
424{
425 int pcount;
426 string* strtest=new string("testimage2.h24");
427
428 cout << "Delete testfs..." << endl;
429 delete testfs;
430 testfs=NULL;
431
432 pass();
433
434 testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
435 delete strtest;
436 cout << "Checking current project count... (should be 50): ";
437
438 pcount=testfs->projectcount();
439 cout << "Expecting project count of 50 again, in reality it is " << pcount << endl;
440 if (pcount!=50) fail("Incorrect project count"); else pass();
441 cout << "Getting existing project no. 50..." << endl;
442 hd24project* currproj=testfs->getproject(50);
443 if (currproj==NULL) { fail("Cannot get project"); } else {pass();}
444 cout << "Getting project name: ";
445 string* pname=currproj->projectname();
446 cout << *pname << endl;
447 if (*pname!="Changed Name") fail("Project name change unsuccessful"); else pass();
448
449// delete rawfs;
450
451}
452
453void test_song()
454{
455 string* strtest=new string("testimage2.h24");
456
457 cout << "Delete testfs..." << endl;
458 delete testfs;
459 testfs=NULL;
460
461 pass();
462
463 testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
464 delete strtest;
465
466 // Check on-disk songcount
467 hd24raw* rawfs=new hd24raw(testfs);
468 int songcount=rawfs->songsondisk();
469 cout << "Checking if song count is still zero as it should be "<<endl;
470 if (songcount!=0)
471 {
472 fail("Song count of newly formatted drive should be 0.");
473 } else {
474 pass();
475 }
476
477 delete rawfs;
478
479}
480
481void test_demoimage()
482{
483 string* strtest=new string("testimage2.h24");
484
485 cout << "Delete testfs..." << endl;
486 delete testfs;
487 testfs=NULL;
488
489 pass();
490
491 testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
492 delete strtest;
493
494 testfs->setvolumename("Sizeable Hard Drive"); // only in memory!!!
495 testfs->commit();
496
497 string* volname=testfs->volumename();
498 if (*volname!="Sizeable Hard Drive") fail(); else pass();
499 delete volname;
500
501 cout << "Checking FS version..." << endl;
502 string* version=testfs->version();
503 if (*version!="1.10") fail("Unexpected FS version (expectation was 1.10)"); else pass();
504 delete version;
505
506 cout << "Checking max. project count (99 expected)..." << endl;
507 __uint32 maxprojcount=testfs->maxprojects();
508 if (maxprojcount!=99) fail("Unexpected max project count"); else pass();
509
510 cout << "Checking block size..." << endl;
511 if (testfs->getblocksizeinsectors() != 0x480) fail("Unexpected block size"); else pass();
512
513 // delete all but last project
514 while (testfs->projectcount()>1)
515 {
516 // leave only last project
517 testfs->deleteproject(2);
518 }
519
520 // now up the total project count to 42
521 char projname[64];
522 int i=1;
523 hd24project* newproj=NULL;
524 while (testfs->projectcount()!=42)
525 {
526 i++;
527 sprintf(projname,"selftest project %d",i+2);
528 int prevprojcount=testfs->projectcount();
529 newproj=testfs->createproject(projname);
530 int newprojcount=testfs->projectcount();
531 if (newprojcount==prevprojcount)
532 {
533 fail("Failed to create project.");
534 break;
535 }
536 if (newproj!=NULL)
537 {
538 delete newproj;
539 newproj=NULL;
540 }
541
542 }
543 if (newproj!=NULL)
544 {
545 delete newproj; // we won't be using this object anymore.
546 newproj=NULL;
547 }
548
549 // rename project now.
550 hd24project* currproj;
551 currproj=testfs->getproject(15); // must error
552 testfs->lastprojectid(15);
553 currproj->projectname("Client X");
554 currproj->save();
555 volname=testfs->volumename();
556 cout <<"Volume name="<<*volname <<endl;
557 testfs->commit();
558 delete currproj;
559 currproj=NULL;
560 currproj=testfs->getproject(15);
561 cout << "Test if project name change with commit works..." << endl;
562 cout << "Projname is supposed to be 'Client X', and is: ";
563 string* sprojname=currproj->projectname();
564 cout << *sprojname << endl;
565 if (*sprojname!="Client X") fail("project name change failed."); else pass();
566 delete sprojname;
567 cout << "Testing if song count is zero..." << endl;
568 if (currproj->songcount()==0) pass(); else fail("Should have been 0");
569
570 // Now it's time to create some songs.
571 for (i=0;i<12;i++)
572 {
573 hd24song* currsong=currproj->createsong("A Huge Hit",24,48000);
574 currsong->save();
575 delete currsong;
576 }
577 currproj->lastsongid(8);
578 currproj->save();
579 testfs->commit();
580 hd24song* currsong=currproj->getsong(currproj->lastsongid());
581 cout << "Renaming song..." << endl;
582 currsong->songname("A Huge Hit (final take)");
583 currsong->save();
584 testfs->commit();
585 delete currsong;
586 currsong=NULL;
587 currsong=currproj->getsong(currproj->lastsongid());
588 cout << "Checking if song ID is equal to last set..." << endl;
589 if (currsong->songid()==8) pass(); else fail("Incorrect songid.");
590 string* freespacebefore=testfs->freespace(48000,24);
591 cout << "Lengthening song..." << endl;
592 __uint32 desiredlen=10416000; //00:03:37.00
593 __uint32 newlen=currsong->songlength_in_samples(desiredlen);
594 if (newlen!=desiredlen)
595 {
596 fail("Song lengthening failed.");
597 cout << "Length in samples is " << newlen
598 << "instead of " << desiredlen << endl;
599 } else pass();
600
601 currsong->setwriteprotected(true);
602 currsong->save();
603 testfs->commit();
604 cout << "Free space on drive before lengthening: " << *freespacebefore << endl;
605 string* freespace=testfs->freespace(48000,24);
606 cout << "Free space on drive after lengthening: " << *freespace << endl;
607 delete freespace;
608}
609
610int runselftest(HD24UserInterface* ui)
611{
612 cout << "=======TESTMODE ACTIVATED=======" << endl;
613 passcount=0;
614 failcount=0;
615 test_createimage();
616 test_useimage();
617 test_commit();
618 test_project();
619 test_proj2();
620
621 test_song();
622 test_demoimage(); // create an interesting looking demo drive image
623
624
625 cout << "Create 1 project, 99 projects, try 100th project" << endl;
626
627 cout << "Create 1..99 songs" << endl;
628
629 cout << "rename projects, rename songs" << endl;
630
631 cout << "Delete 1.99 projects/songs" << endl;
632
633 cout << "Record audio to songs" << endl;
634
635 // Proceed to test gui.
636
637 Fl_Window* window=ui->make_window(testfs);
638 window->end();
639 window->show();
640 fl_message("Proceeding to test GUI.");
641 cout << "====================== TEST COMPLETE ===================" << endl;
642 cout << "PASS: " << passcount << endl;
643 cout << "FAIL: " << failcount << endl;
644 cout << "========================================================" << endl;
645 return 0;
646}