aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/selftest.cpp
blob: 2f3f65a30b3c33be24eee0de8e89d681692ed33a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include <FL/forms.H>
hd24fs* testfs;

void createimage_callback(void* y)
{
	cout << "." << endl;
	Fl::add_timeout(1.0,createimage_callback,y);	
	return;
}
int passcount;
int failcount;
#define MINSECTORS 1353964

//#define BITBIGGER (MINSECTORS+10000)
/* this gives for a bit over 5 minutes at 48k, 24 tracks: */
#define BITBIGGER (MINSECTORS+2073600)

/* constant is for pretty demo image. */
void pass()
{
	passcount++;
	cout << "......................................................PASS" << endl;
}
void fail()
{
	failcount++;
	cout << "......................................................FAIL" << endl;
}
void fail(const char* reason)
{
	failcount++;
	cout << "......................................................FAIL" << endl;
	cout << "Reason: "<<reason << endl;
}
void test_createimage()
{
	cout << "Test if creating too small drive images is prevented... " << endl;

	__uint32 lastsector=MINSECTORS-1;

	string* strtest=new string("testimage.h24");
	char message[2048];
	message[0]='\0';
   	Fl::add_timeout(1.0,createimage_callback,&message[0]);
	int result=hd24utils::newdriveimage(strtest,lastsector,&message[0],NULL);
	delete strtest;
	Fl::remove_timeout(&createimage_callback);
	if (result!=-1) fail(); else pass();
	////////////////////////////////////////////////////
	cout << "-----------------------------------------------" << endl;
	cout << "Create minimum size empty drive image... please wait" << endl;
	lastsector=MINSECTORS;

	strtest=new string("testimage.h24");
	result=hd24utils::newdriveimage(strtest,lastsector,&message[0],NULL);
	
	cout << "Image create messages=" << message << endl;
	if (result==0) pass(); else fail();
	// Check expected drive image contents.
	hd24fs* testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
	/* we calculate free space based on 1 channel because it is much
	   more sensitive than free space based on 24 channels */
	string* freespace=testfs->freespace(48000,1); 
							
	cout << "Free space on drive: " << *freespace << endl;
	delete testfs;
	//////////////////////////////////////////////////////
	cout << "-----------------------------------------------" << endl;
	cout << "Creating somewhat larger size empty drive image... please wait" << endl;
	lastsector=BITBIGGER;
		
	string* strtest2=new string("testimage2.h24");
	result=hd24utils::newdriveimage(strtest2,lastsector,&message[0],NULL);
	
	cout << "Image create messages=" << message << endl;
	if (result==0) pass(); else fail();
	// Check expected drive image contents.
	hd24fs* testfs2=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest2,false);
	string* freespace2=testfs2->freespace(48000,1);
	cout << "Free space on drive: " << *freespace2 << endl;
	if (strcmp(freespace->c_str(),freespace2->c_str())==0)
	{
		fail("Images were supposed to have different sizes");
	} else {
		pass();
	}
	delete testfs2;
	delete freespace2;
	delete strtest2;
//*/
	delete freespace;
	delete strtest;
	return;
}

void test_useimage()
{
	char failmsg[64];
	cout << "Try to use empty drive image... " << endl;
	//false=force?
	string* strtest=new string("testimage2.h24");
	testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
	delete strtest;
	strtest=NULL;

	// Let's see if all is as expected.
	if (!(testfs->isOpen())) fail(); else pass();
	string* freespace=testfs->freespace(48000,24);
	cout << "Free space on drive: " << *freespace << endl;
	delete freespace;
	hd24raw* rawfs=new hd24raw(testfs);
	/*
		These functions are only available through raw FS
	*/
	int songsondisk=rawfs->songsondisk();
	cout << "Songs on disk=" << songsondisk << endl;
	if (songsondisk!=0) fail(); else pass();

	__uint32 lastsector=rawfs->getlastsectornum();
	__uint32 expectedlastsector=BITBIGGER; 
	cout << "Last sectornum of drive image="<< lastsector << endl;
	
	
	if (lastsector!=expectedlastsector)
	{
		sprintf(failmsg,"Expected last sector was %ld but found %ld",expectedlastsector,lastsector);
		fail(failmsg);
	}
	else
	{
		pass();
	}

	string* devname=testfs->getdevicename(); 
	cout <<"Device name="<<*devname<<endl;
	if (*devname=="")
	{
		fail("Device name should always exist");
	} else pass();

	string* volname=testfs->volumename();
	cout <<"Volume name="<<*volname <<endl;
	if (*volname=="") fail("Volume name of freshly formatted drive must be nonempty"); else pass();

	// set volume name, commit, then test if change OK
	__uint32 projcount=testfs->projectcount();
	cout <<"Project count="<<projcount << endl;
	if (projcount!=1) fail("Newly formatted image must have exactly 1 project."); else pass();
	hd24project* currproj=NULL;
	if (projcount>0)
	{
		currproj=testfs->getproject(0); // must error
		cout << "Check protection against getting project 0" << endl;
		if (currproj!=NULL) fail(); else pass();

		delete currproj;
		currproj=testfs->getproject(1); // must not error
		cout << "Check zero songcount for supposedly empty project" << endl;
		__uint32 songcount=currproj->songcount();
		if (songcount!=0) fail(); else pass();

		cout << "Check getting nonexisting/invalid project numbers" << endl;
		int failed=0;
		for (int i=2; i<=100; i++)	
		{
			currproj=testfs->getproject(i); // must not error but must return null
			if (currproj!=NULL)
			{
				failed=1;
			}
		}
		if (failed!=0) fail(); else pass();
	}

	cout << "set volume name in drive image" << endl;	
	testfs->setvolumename("myvol"); // only in memory!!!
	// check if volume name has been set (in memory)
	cout << "reading back volume name to see if set was successful" << endl;

	volname=testfs->volumename();
	if (*volname!="myvol") fail(); else pass();

	cout << "set longer name in drive image" << endl;	
	testfs->setvolumename("mylongervolumename"); // only in memory!!!
	volname=testfs->volumename();
	if (*volname!="mylongervolumename") fail(); else pass();

	cout << "Test truncating too long volume name in drive image" << endl;
	testfs->setvolumename("myoverlylongvolumenamethemaximumissixtyfourcharactersbutwemakeitlongeranyway"); // only in memory!!!
	volname=testfs->volumename();
	if (strlen(volname->c_str())!=64) fail(); else pass();
	cout << "Try to clear volume name" <<endl;
	testfs->setvolumename("");
	volname=testfs->volumename();
	if (strlen(volname->c_str())!=0) fail(); else pass();
	cout << "Setting volume name " << endl;
	testfs->setvolumename("MyVolume");
	cout << "and commiting for later readback." << endl;	
	testfs->savedriveinfo(); // auto-commit
	cout << "Commit done." << endl;
	return;
}

void test_commit()
{
	string* strtest=new string("testimage2.h24");

	cout << "Delete testfs..." << endl;
	delete testfs;
	testfs=NULL;

	pass();

	testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
	delete strtest;
	cout << "Reading back volume name" << endl;
	string* volname=testfs->volumename();
	cout << "Read back " << *volname << endl;
	if (*volname!="MyVolume") fail(); else pass();

	// after commit, drive size must be preserved.
	hd24raw* rawfs=new hd24raw(testfs);
	__uint32 lastsector=rawfs->getlastsectornum();
	__uint32 expectedlastsector=BITBIGGER;
 	cout << "Test if image has remained same size: "<< lastsector << endl;
	if (lastsector!=expectedlastsector) fail(); else pass();
	

	testfs->setvolumename("Drive Name");
	testfs->savedriveinfo(); // commit tested before-assume it succeeds.
}

void test_project()
{
	string* strtest=new string("testimage2.h24");

	cout << "Delete testfs..." << endl;
	delete testfs;
	testfs=NULL;
	
	pass();

	testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
	delete strtest;
	hd24raw* rawfs=new hd24raw(testfs);
	cout << "Checking current project count... (should be 1): ";
	int pcount=testfs->projectcount();	
	cout << pcount << endl;
	if (pcount==1) pass(); else fail("Project count of formatted drive should be 1");

	cout << "Try to get project id 0 (should not be possible)" << endl;
	hd24project* proj=testfs->getproject(0);
	if (proj==NULL) pass(); else
	{
		delete proj;
		proj=NULL;
		fail("Project IDs should be 1 based but are not!");
	}

	cout << "Try to get project id 1 (first and only project on drive" << endl;
	proj=testfs->getproject(1);
	if (proj==NULL) fail("Cannot get only project on drive"); else
	{
		delete proj;
		proj=NULL;
		pass();
	}
	cout << "Try to get any but the first project on the drive" << endl;
	cout << "(using legal project numbers and one too high number)" << endl;
	int projsgot=0;
	int i=0;
	for (i=2; i<=100; i++)
	{
		if (proj!=NULL)
		{
			delete proj;
			proj=NULL;
		}
		proj=testfs->getproject(i);
		if (proj!=NULL)
		{
			projsgot++;
			fail("Managed to get project number:");
			cout << i << endl;
			delete proj;
			proj=NULL;
		}
	}
	if (proj!=NULL)
	{
		delete proj;
		proj=NULL;
	}

	if (projsgot==0) pass();

	cout << "Try if only project on drive is protected from deleting" << endl;
	testfs->deleteproject(1);
	pcount=testfs->projectcount();	
	cout << pcount << endl;
	if (pcount==1) pass(); else fail("Only project on drive should not be deletable");

	cout << "Try to create a project..." << endl;
	hd24project* newproj=testfs->createproject("selftest project 1");
	if (newproj==NULL)
	{
		fail("Creating project failed");
	} else {
		pass();
	}
	delete newproj; // we won't be using this object anymore.

	cout << "Make sure project creation updates project count..." << endl;
	pcount=testfs->projectcount();	
	if (pcount==2) pass(); else fail("Project count should have been 2 at this stage.");
	cout << "Trying to delete newly created project..." << endl;
	testfs->deleteproject(2);
	pcount=testfs->projectcount();	
	if (pcount==1) pass(); else fail("Deletion successful");

	cout << "Trying to create 98 new projects (should be possible)..." << endl;
	char projname[64];
	int failedcreate=0;	
	for (i=0;i<98;i++)
	{
		cout << "project=" << i << endl;
		sprintf(projname,"selftest project %d",i+2);
		hd24project* newproj=testfs->createproject(projname);
		pcount=testfs->projectcount();	
		if (pcount!=(i+2))
		{
			sprintf(projname,"could not create project %d",i);
			fail(projname);
			failedcreate=1;
			break;
		};
		delete newproj;
	}
	if (failedcreate==0)
	{
		pass();
	}
	cout << "Checking if project count is 99 as expected " << endl;
	pcount=testfs->projectcount();	
	if (pcount==99) pass(); else fail("Nope.");

	cout << "Trying if creating more projects than allowed is prevented" << endl;
	newproj=NULL;
	newproj=testfs->createproject(projname);
	if (newproj!=NULL)
	{
		fail("Project object was returned!");
	} else {
		pass();
	}
	
	pcount=testfs->projectcount();	
	cout << "Verifying project count to still be 99" <<endl;
	if (pcount==99) pass(); else fail("Project count unduely updated.");

	cout << "Deleting last 50 projects" << endl;
	/* this should leave 49 projects in place. */
	for (i=1;i<60;i++)
	{
		testfs->deleteproject(50);
	}
	pcount=testfs->projectcount();	
	if (pcount==49) pass(); else fail("Incorrect project count.");
	cout << "Creating 50th project again" << i << endl;
	sprintf(projname,"selftest project %d",i+2);
	if (newproj!=NULL) delete newproj;

	newproj=testfs->createproject(projname);
	pcount=testfs->projectcount();
	cout << "Expecting project count to be 50 now..." << endl;	
	if (pcount!=50) fail("Incorrect project count"); else pass();

	cout << "Checking project sector of first project (must be 0x14 or 20)" << endl;
	__uint32 projsec= rawfs->getprojectsectornum(1);
	cout << "Proj sector=" << projsec << endl;
	if (projsec!=0x14) fail("Incorrect sector"); else pass();
	
	cout << "Deleting first project (to empty a slot)" << endl;
	pcount=testfs->projectcount();	
	cout << "project count BEFORE=" <<pcount <<endl;

	testfs->deleteproject(1);
	int pcount2=testfs->projectcount();
	cout << "Project count AFTER=" <<pcount2 << endl;	
	if (pcount2!=(pcount-1)) fail("Expected project count to be 49."); else pass();
	projsec= rawfs->getprojectsectornum(1);
	cout << "Second project should originally have been on 0x15,"<<endl;
	cout << "expecting first project to start on that sector now..." <<endl;
	cout << "(in reality it is on " << projsec << ")" << endl;
	if (projsec!=0x15) fail("Incorrect sector"); else pass();
	/* now create a new project-- this project must be on sector 0x14
	again */
	cout << "Creating new project - it should overwrite the first project block" << endl;
	newproj=testfs->createproject(projname);
	pcount=testfs->projectcount();	
	cout << "Expecting project count of 50 again, in reality it is " << pcount << endl;
	if (pcount!=50) fail("Incorrect project count"); else pass();
	__sint32 projid=newproj->projectid();
	cout << "Project id=" << projid << endl;
	if (projid!=50) {
		fail("Unexpected project id");
	}
	projsec=rawfs->getprojectsectornum(projid);
	cout << "Expecting project sector of project " << projid << " to be 0x14," ;
	cout << " in reality it is " << projsec << endl;

	if (projsec!=0x14) fail("Incorrect sector"); else pass();

	newproj->projectname("Changed Name");
	string* projcname=newproj->projectname();
	if (*projcname!="Changed Name") fail("Project name change unsuccessful"); else pass();
	delete projcname;
	newproj->save();
	delete rawfs;		
}


void test_proj2()
{
	int pcount;
	string* strtest=new string("testimage2.h24");

	cout << "Delete testfs..." << endl;
	delete testfs;
	testfs=NULL;

	pass();

	testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
	delete strtest;
	cout << "Checking current project count... (should be 50): ";

	pcount=testfs->projectcount();	
	cout << "Expecting project count of 50 again, in reality it is " << pcount << endl;
	if (pcount!=50) fail("Incorrect project count"); else pass();
	cout << "Getting existing project no. 50..." << endl;
	hd24project* currproj=testfs->getproject(50);
	if (currproj==NULL) { fail("Cannot get project"); } else {pass();}
	cout << "Getting project name: ";
	string* pname=currproj->projectname();
	cout << *pname << endl;
	if (*pname!="Changed Name") fail("Project name change unsuccessful"); else pass();
	
//	delete rawfs;		
	
}

void test_song()
{
	string* strtest=new string("testimage2.h24");

	cout << "Delete testfs..." << endl;
	delete testfs;
	testfs=NULL;

	pass();

	testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
	delete strtest;

	// Check on-disk songcount
	hd24raw* rawfs=new hd24raw(testfs);
	int songcount=rawfs->songsondisk();
	cout << "Checking if song count is still zero as it should be "<<endl;
	if (songcount!=0)
	{
		fail("Song count of newly formatted drive should be 0.");
	} else {
		pass();
	}

	delete rawfs;		
	
}

void test_demoimage()
{
	string* strtest=new string("testimage2.h24");

	cout << "Delete testfs..." << endl;
	delete testfs;
	testfs=NULL;

	pass();

	testfs=new hd24fs((const char*)NULL,hd24fs::MODE_RDWR,strtest,false);
	delete strtest;
	
	testfs->setvolumename("Sizeable Hard Drive"); // only in memory!!!
	testfs->commit();
	
	string* volname=testfs->volumename();
	if (*volname!="Sizeable Hard Drive") fail(); else pass();
	delete volname;

	cout << "Checking FS version..." << endl;
	string* version=testfs->version();
	if (*version!="1.10") fail("Unexpected FS version (expectation was 1.10)"); else pass();
	delete version;

	cout << "Checking max. project count (99 expected)..." << endl;
	__uint32 maxprojcount=testfs->maxprojects();
	if (maxprojcount!=99) fail("Unexpected max project count"); else pass();

	cout << "Checking block size..." << endl;
	if (testfs->getblocksizeinsectors() != 0x480) fail("Unexpected block size"); else pass();

	// delete all but last project
	while (testfs->projectcount()>1)
	{
		// leave only last project	
		testfs->deleteproject(2);
	}

	// now up the total project count to 42	
	char projname[64];
	int i=1;
	hd24project* newproj=NULL;
	while (testfs->projectcount()!=42)
	{
		i++;
		sprintf(projname,"selftest project %d",i+2);
		int prevprojcount=testfs->projectcount();
		newproj=testfs->createproject(projname);
		int newprojcount=testfs->projectcount();
		if (newprojcount==prevprojcount)
		{
			fail("Failed to create project.");
			break;
		}
		if (newproj!=NULL) 
		{
			delete newproj;
			newproj=NULL;
		}
			
	}
	if (newproj!=NULL)
	{
		delete newproj; // we won't be using this object anymore.
		newproj=NULL;
	}

	// rename project now.
	hd24project* currproj;
	currproj=testfs->getproject(15); // must error
	testfs->lastprojectid(15);
	currproj->projectname("Client X");
	currproj->save();
	volname=testfs->volumename();
	cout <<"Volume name="<<*volname <<endl;
	testfs->commit();
	delete currproj;
	currproj=NULL;
	currproj=testfs->getproject(15);
	cout << "Test if project name change with commit works..." << endl;
	cout << "Projname is supposed to be 'Client X', and is: ";
	string* sprojname=currproj->projectname();
	cout << *sprojname << endl;
	if (*sprojname!="Client X") fail("project name change failed."); else pass();
	delete sprojname;
	cout << "Testing if song count is zero..." << endl;
	if (currproj->songcount()==0) pass(); else fail("Should have been 0");

	// Now it's time to create some songs.
	for (i=0;i<12;i++)
	{
		hd24song* currsong=currproj->createsong("A Huge Hit",24,48000);
		currsong->save();
		delete currsong;
	}
	currproj->lastsongid(8);
	currproj->save();
	testfs->commit();
	hd24song* currsong=currproj->getsong(currproj->lastsongid());
	cout << "Renaming song..." << endl;
	currsong->songname("A Huge Hit (final take)");
	currsong->save();
	testfs->commit();
	delete currsong;
	currsong=NULL;
	currsong=currproj->getsong(currproj->lastsongid());
	cout << "Checking if song ID is equal to last set..." << endl;
	if (currsong->songid()==8) pass(); else fail("Incorrect songid.");
	string* freespacebefore=testfs->freespace(48000,24);
	cout << "Lengthening song..." << endl;
	__uint32 desiredlen=10416000; //00:03:37.00
	__uint32 newlen=currsong->songlength_in_samples(desiredlen);
	if (newlen!=desiredlen)
	{
		fail("Song lengthening failed.");  
		cout << "Length in samples is " << newlen 
		     << "instead of " << desiredlen << endl;
	} else pass();
	
	currsong->setwriteprotected(true);
	currsong->save();
	testfs->commit();
	cout << "Free space on drive before lengthening: " << *freespacebefore << endl;
	string* freespace=testfs->freespace(48000,24);
	cout << "Free space on drive after lengthening: " << *freespace << endl;
	delete freespace;
}

int runselftest(HD24UserInterface* ui)
{
	cout << "=======TESTMODE ACTIVATED=======" << endl;
	passcount=0;
	failcount=0;
	test_createimage();
	test_useimage();
	test_commit();
	test_project();
	test_proj2();

	test_song();
	test_demoimage(); // create an interesting looking demo drive image


	cout << "Create 1 project, 99 projects, try 100th project" << endl;

	cout << "Create 1..99 songs" << endl;

	cout << "rename projects, rename songs" << endl;

	cout << "Delete 1.99 projects/songs" << endl;

	cout << "Record audio to songs" << endl;
	
	// Proceed to test gui.

	Fl_Window* window=ui->make_window(testfs);
	window->end();
	window->show();
	fl_message("Proceeding to test GUI.");
	cout << "====================== TEST COMPLETE ===================" << endl;
	cout << "PASS: " << passcount << endl;
	cout << "FAIL: " << failcount << endl;
	cout << "========================================================" << endl;
	return 0;
}