int main( int argc, char *argv[] ){ int numlines, k,s,l,i ; int choice ; // the number of the test addresses const int test_num = 39 ; int test[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 0, 4, 8, 12, 16, 71, 3, 41, 81, 39, 38, 71, 15, 39, 11, 51, 57, 41 } ; //hit_miss is a record if each address, when it is checked in cache char* hit_miss[test_num] ; printf( "Enter the size of cache (in byte): " ) ; scanf("%d", &s ) ; printf( "Enter the size of a cache's line (in byte): " ) ; scanf("%d", &l ) ; printf( "Choose one of these options: \n" ) ; printf(" 1: Direct Map Cache\n 2: Fully Associative Cache\n 3: K-way Associative Cache\n " ) ; scanf( " %d" ,&choice ) ; switch( choice ){ /* a direct-mapped cache corresponds to a 1-way set associative cache (n=1), and a fully associative corresponds to a n-way set associative cache with n=s/l, the number of rows (lines) in the cache. */ case 1: check_direct(test, hit_miss, s, l, test_num ) ; break ; case 2: //fully associative has the number of k-way = s / l check_k_way(test, hit_miss, (s/l) ,s, l, test_num) ; break ; case 3: printf(" Enter how many k-way this cache has: " ) ; scanf( "%d", &k ) ; check_k_way(test, hit_miss, k, s, l, test_num ) ; break ; default: printf( "Bye \n " ) ; break ; } for( i = 0 ; i < test_num; i++ ){ printf( "Address: %d \t %s\n", test[i], hit_miss[i]); } printf("\n\n") ; return 0 ; }
template <class Val, class Idx> static int check_direct( _CvTreeNode<Val,Idx>* root ) { if( root == 0 ) return TRS_OK; int res = TRS_OK; if( root->link[_CvTreeNode<Val,Idx>::left] != 0 ) { if( root->link[_CvTreeNode<Val,Idx>::left]->idx > root->idx ) { trsWrite( ATS_CON | ATS_LST, "Wrong direct: act: left, exp: right, %d\n", (int)root->idx ); res = TRS_FAIL; } if( root->link[_CvTreeNode<Val,Idx>::left]->idx == root->idx ) { trsWrite( ATS_CON | ATS_LST, "Wrong direct: act: left, exp: no_branch, %d\n", (int)root->idx ); res = TRS_FAIL; } if( root->link[_CvTreeNode<Val,Idx>::left]->link[_CvTreeNode<Val,Idx>::up] != root ) { trsWrite( ATS_CON | ATS_LST, "Wrong link (left branch), %d\n", (int)root->idx ); res = TRS_FAIL; } } if( root->link[_CvTreeNode<Val,Idx>::right] != 0 ) { if( root->link[_CvTreeNode<Val,Idx>::right]->idx < root->idx ) { trsWrite( ATS_CON | ATS_LST, "Wrong direct: act: right, exp: left, %d\n", (int)root->idx ); res = TRS_FAIL; } if( root->link[_CvTreeNode<Val,Idx>::right]->idx == root->idx ) { trsWrite( ATS_CON | ATS_LST, "Wrong direct: act: right, exp: no_branch, %d\n", (int)root->idx ); res = TRS_FAIL; } if( root->link[_CvTreeNode<Val,Idx>::right]->link[_CvTreeNode<Val,Idx>::up] != root) { trsWrite( ATS_CON | ATS_LST, "Wrong link (right branch), %d\n", (int)root->idx ); res = TRS_FAIL; } } if( res != TRS_OK ) return res; if( check_direct( root->link[_CvTreeNode<Val,Idx>::left] ) != TRS_OK || check_direct( root->link[_CvTreeNode<Val,Idx>::right] ) != TRS_OK ) return TRS_FAIL; else return TRS_OK; }
template <class Val, class Idx> static int foaCvTree( Val, Idx ) { CvTree<Val, Idx> tree; int res = TRS_OK; /* Check adding new node (operator[]) */ for( Idx i = 0; i < hsize; i++ ) { Idx num = (Idx)atsInitRandom( 0, hsize ); tree[num] = (Val)i; if( check_direct( tree.get_root() ) != TRS_OK || check_balance( tree.get_root() ) < 0 ) { return trsResult( TRS_FAIL, "Error adding new node" ); } } /* Check getting node */ for( i = 0; i < hsize; i++ ) tree[i] = (Val)i; for( i = 0; i < hsize; i++ ) if( tree[i] != (Val)i ) { trsWrite( ATS_CON | ATS_LST, "error operator[] (get node)\n" ); res = TRS_FAIL; break; } for( i = 0; i < hsize; i++ ) if( tree.query(i) != (Val)i ) { trsWrite( ATS_CON | ATS_LST, "error query() function\n" ); res = TRS_FAIL; break; } tree.clear(); for( i = 0; i < hsize; i++ ) tree[i] = (Val)i; for( i = 0; i < hsize; i++ ) { Idx num = (Idx)atsInitRandom( 0, hsize ); tree.remove( num ); if( check_direct( tree.get_root() ) != TRS_OK || check_balance( tree.get_root() ) < 0 ) { return trsResult( TRS_FAIL, "Error removing node" ); } if( tree.query( num ) != 0 ) { trsWrite( ATS_CON | ATS_LST, "Error remove() function (not null) \n" ); res = TRS_FAIL; } } for( i = 0; i < hsize / 2; i++ ) { tree.remove( i ); if( check_direct( tree.get_root() ) != TRS_OK || check_balance( tree.get_root() ) < 0 ) { return trsResult( TRS_FAIL, "Error removing node" ); } if( tree.query( i ) != 0 ) { trsWrite( ATS_CON | ATS_LST, "Error remove() function (not null) \n" ); res = TRS_FAIL; } } for( i = hsize; i > hsize / 2 - 1; i-- ) { tree.remove( i ); if( check_direct( tree.get_root() ) != TRS_OK || check_balance( tree.get_root() ) < 0 ) { return trsResult( TRS_FAIL, "Error removing node" ); } if( tree.query( i ) != 0 ) { trsWrite( ATS_CON | ATS_LST, "Error remove() function (not null) \n" ); res = TRS_FAIL; } } tree.clear(); for( i = 0; i < hsize; i++ ) tree[i] = (Val)(i + 1); CvTree<Val, Idx>::iterator iterator = tree.begin(); if( *iterator != 1 ) { trsWrite( ATS_CON | ATS_LST, "Wrong begin() function (bad val)\n"); res = TRS_FAIL; } if( iterator.get_idx() != 0 ) { trsWrite( ATS_CON | ATS_LST, "Wrong begin() function (bad idx)\n"); res = TRS_FAIL; } iterator = tree.end(); if( *iterator != hsize ) { trsWrite( ATS_CON | ATS_LST, "Wrong end() function (bad val)\n"); res = TRS_FAIL; } if( iterator.get_idx() != hsize - 1 ) { trsWrite( ATS_CON | ATS_LST, "Wrong end() function (bad idx)\n"); res = TRS_FAIL; } CvTree<Val, Idx> empty; empty = tree; CvTree<Val, Idx>::iterator beg1 = empty.begin(); CvTree<Val, Idx>::iterator beg2 = tree.begin(); CvTree<Val, Idx>::iterator end = tree.begin(); while( beg2 != end ) { if( *beg2 != *beg1 ) { res = TRS_FAIL; trsWrite( ATS_CON | ATS_LST, "Wrong operator= (bad value)\n" ); break; } if( beg2.get_idx() != beg1.get_idx() ) { res = TRS_FAIL; trsWrite( ATS_CON | ATS_LST, "Wrong operator= (bad idx)\n" ); break; } beg1++; beg2++; } if( *beg2 != *beg1 ) { res = TRS_FAIL; trsWrite( ATS_CON | ATS_LST, "Wrong operator= (bad value)\n" ); } if( beg2.get_idx() != beg1.get_idx() ) { res = TRS_FAIL; trsWrite( ATS_CON | ATS_LST, "Wrong operator= (bad idx)\n" ); } return res; }