Exemple #1
0
//-------------------------------------------------------------
// test
//-------------------------------------------------------------
Status test(int t)
{
	char		outfile[10];
	int		Rarray[] = { 0, 1, 0, 3, 4, 4, 4 };
	int		Sarray[] = { 1, 0, 2, 2, 2, 3, 0 };
	int 		R = Rarray[t-1];
	int 		S = Sarray[t-1];
	Status		s;

	sprintf(outfile,"test%d", t);

	// Perform sort-merge on R and S
	sortMerge	sm(files[R],NUM_COLS,attrType,attrSize,JOIN_COL,files[S],NUM_COLS,attrType,attrSize,JOIN_COL,outfile,SORTPGNUM,Ascending,s);
	if (s != OK)
	{
		cout << "Test " << t << " -- sortMerge failed" << endl;
		return s;
	}

	// Write merged results to stdout
	HeapFile*	outf = new HeapFile (outfile,s);
	if (s != OK)
	{
		cout << "Test " << t << " -- result file not created " << endl;
		return s;
	}
        Scan*	scan = outf->openScan(s);
	assert(s == OK);
	int len;
	RID	rid;
	char	rec[sizeof(struct _rec)*2];
	cout << endl;
	cout << "------------ Test " << t << " ---------------" << endl;
	for (s = scan->getNext(rid, rec, len); s == OK; s = scan->getNext(rid, rec, len)) 
	{
	  cout << (*((struct _rec*)&rec)).key << "\t" << (*((struct _rec*)&rec[8])).key << endl;
	}
	cout << "-------- Test " << t << " completed --------" << endl;
	delete scan;
	s=outf->deleteFile();
	if(s!=OK) MINIBASE_CHAIN_ERROR(JOINS,s);
	delete outf;
	return s;
}
Exemple #2
0
//-------------------------------------------------------------
// test2
//-------------------------------------------------------------
void test2()
{
	struct R1 {
		char	key [32];
	} rec; 

	AttrType 	attrType[] = { attrString };
	short		attrSize[] = { 32 };

	// Create unsorted data file "test2.in"
	Status		s;
	RID		rid;
	HeapFile	f("test2.in",s);
	assert(s == OK);
	for (int i=0; i<NUM_RECORDS; i++)
	{
		strcpy(rec.key, data2[i]);
		s = f.insertRecord((char*)&rec,REC_LEN2,rid);
		assert(s == OK);
	}

	// Sort "test2.in" into "test2.out"
	keySize = attrSize[0];
	sortOrder = Ascending;
	Sort 		sort("test2.in","test2.out",1,attrType,attrSize,0,Ascending,SORTPGNUM,s);
	f.deleteFile();
	if (s != OK)
	{
		minibase_errors.show_errors();
		minibase_errors.clear_errors();
		return;
	}

	// check if "test2.out" is sorted correctly
	HeapFile	f2("test2.out",s);
	if (s != OK)
	{
		cout << "Test2 -- OOPS! test2.out not created\n";
		return;
	}
	Scan*	scan = f2.openScan(s);
	assert(s == OK);
	int len;
	int count = 0;
	for (s = scan->getNext(rid, (char *) &rec, len); 
		(s == OK) && (count < NUM_RECORDS);
		s = scan->getNext(rid, (char *) &rec, len)) 
	{
		if (strcmp(rec.key,data2[count]) != 0)
		{
			cout << "Test2 -- OOPS! test2.out not sorted\n";
			s = FAIL;
		}
		count++;
	}
	if (count != NUM_RECORDS)
		cout << "Test2 -- OOPS! Sorting Failed\n";
	else
		cout << "Test2 -- Sorting OK\n";
	f2.deleteFile();
}