示例#1
0
文件: reformat1.c 项目: clr/cos472
/*****************************************************************************
  We run the program by calling something along the lines of:
  guessMushroom -train mushrooms.data -test moreMushrooms.data -out mushrooms.out 
 ******************************************************************************/
int main(int argc, char* argv[]) {
  MushroomP trainingShroomList;
  trainingShroomList = NULL;
  MushroomP testingShroomList;
  testingShroomList = NULL;
  ValueP availableValues;
  DecisionP decisionTree;
  availableValues = NULL;
  decisionTree = NULL;
  char infilenameTraining[STRLEN]; /* input file name for training data */
  char infilenameTesting[STRLEN]; /* input file name for testing data */
  char outfilename[STRLEN]; /* output file name for printing the results */
 
  printf( "This program processes data about the attributes mushrooms.\n" );
  
  /* initialize filenames to dummy strings */
  strcpy( infilenameTraining, "none" );
  strcpy( infilenameTesting, "none" );
  strcpy( outfilename, "none" );
  /* TAKEN FROM p5.c EXAMLPE
     process command-line arguments */
  while( *++argv ) {             /* while there are still command-line args */
    if ( !strcmp( *argv, "-train" ) )          /* set input file name */
      strcpy( infilenameTraining, *++argv );
    else if( !strcmp( *argv,"-test" ) )          /* set input file name */
      strcpy( infilenameTesting, *++argv );
    else if( !strcmp( *argv,"-out" ) )     /* set output file name */
      strcpy( outfilename,*++argv );
    else {  /*  error checking */
      printf( "\nERROR: option %s not recognized\n", *argv );
      printf( "...exiting program\n\n" );
      exit( 0 );
    }
  } /* end, while there are still command-line args */
 
  /* this is the training data */
  trainingShroomList = readMushrooms( infilenameTraining );
 
  /* this is the testing data */
  testingShroomList = readMushrooms( infilenameTesting );
 
  availableValues = findAvailableValues( 'e', 0, trainingShroomList );
 
  decisionTree = createDecisionTree( 'e', 0, trainingShroomList, availableValues );
 
  printDecisionTree( decisionTree, outfilename );
 
  printTestResults( 'e', 0, decisionTree, testingShroomList, outfilename );

  /* free the list of test mushrooms */
  freeMushrooms( trainingShroomList );
  freeMushrooms( testingShroomList );
  /* note that Decision structs are freed in printDecisionTree */
  /* free value list */
  freeValues( availableValues );

	return (0);
 
} /* end main */
示例#2
0
SelectionTreeElement::~SelectionTreeElement()
{
    /* Free the children.
     * Must be done before freeing other data, because the children may hold
     * references to data in this element. */
    child.reset();

    freeValues();
    freeExpressionData();
    freeCompilerData();
}
示例#3
0
void
testPrintTree(void)
{
  RID insert[] = { 
    {1,1},
    {2,3},
    {1,2},
    {3,5},
    {4,4},
    {3,2}, 
  };
  int numInserts = 6;
  Value **keys;
  char *stringKeys[] = {
    "i1",
    "i11",
    "i13",
    "i17",
    "i23",
    "i52"
  };
  testName = "test b-tree print";
  int i, testint;
  BTreeHandle *tree = NULL;
  
  keys = createValues(stringKeys, numInserts);

  // init
  TEST_CHECK(initIndexManager(NULL));
  TEST_CHECK(createBtree("testidx", DT_INT, 2));
  TEST_CHECK(openBtree(&tree, "testidx"));
       printf("0"); // remove it later

  // insert keys
  for(i = 0; i < numInserts; i++)
    TEST_CHECK(insertKey(tree, keys[i], insert[i]));

  
  char * expected = "(0)[1,13,2,23,3]\n(1)[1.1,1,2.3,11,2]\n(2)[1.2,13,3.5,17,3]\n(3)[4.4,23,3.2,52]\n";

  //test printTree function
  ASSERT_EQUALS_STRING(expected,printTree(tree),"checking b-tree shape");			\

  // cleanup
  TEST_CHECK(closeBtree(tree));
  TEST_CHECK(deleteBtree("testidx"));
  TEST_CHECK(shutdownIndexManager());
  freeValues(keys, numInserts);

  TEST_DONE();
}
// ************************************************************ 
void
testInsertAndFind (void)
{
  RID insert[] = { 
    {1,1},
    {2,3},
    {1,2},
    {3,5},
    {4,4},
    {3,2}, 
  };
  int numInserts = 6;
  Value **keys;
  char *stringKeys[] = {
    "i1",
    "i11",
    "i13",
    "i17",
    "i23",
    "i52"
  };
  testName = "test b-tree inserting and search";
  int i, testint;
  BTreeHandle *tree = NULL;
  
  keys = createValues(stringKeys, numInserts);

  // init
  TEST_CHECK(initIndexManager(NULL));
  printf("\n Init");
  TEST_CHECK(createBtree("testidx", DT_INT, 2));
  printf("\n Created");
 TEST_CHECK(openBtree(&tree, "testidx"));

  // insert keys
  for(i = 0; i < numInserts; i++)
    TEST_CHECK(insertKey(tree, keys[i], insert[i]));

  // check index stats
  TEST_CHECK(getNumNodes(tree, &testint));
  ASSERT_EQUALS_INT(testint,4, "number of nodes in btree");
  TEST_CHECK(getNumEntries(tree, &testint));
  ASSERT_EQUALS_INT(testint, numInserts, "number of entries in btree");

  // search for keys
  for(i = 0; i < 1000; i++)
    {
      int pos = rand() % numInserts;
      RID rid;
      Value *key = keys[pos];

      TEST_CHECK(findKey(tree, key, &rid));
      ASSERT_EQUALS_RID(insert[pos], rid, "did we find the correct RID?");
    }

  // cleanup
  TEST_CHECK(closeBtree(tree));
  TEST_CHECK(deleteBtree("testidx"));
  TEST_CHECK(shutdownIndexManager());
  freeValues(keys, numInserts);
  

  TEST_DONE();
}
// ************************************************************ 
void
testIndexScan (void)
{
  RID insert[] = { 
    {1,1},
    {2,3},
    {1,2},
    {3,5},
    {4,4},
    {3,2}, 
  };
  int numInserts = 6;
  Value **keys;
  char *stringKeys[] = {
    "i1",
    "i11",
    "i13",
    "i17",
    "i23",
    "i52"
  };
  
  testName = "random insertion order and scan";
  int i, testint, iter, rc;
  BTreeHandle *tree = NULL;
  BT_ScanHandle *sc = NULL;
  RID rid;
  
  keys = createValues(stringKeys, numInserts);

  // init
  TEST_CHECK(initIndexManager(NULL));

  for(iter = 0; iter < 50; iter++)
    {
      int *permute;

      // create permutation
      permute = createPermutation(numInserts);

      // create B-tree
      TEST_CHECK(createBtree("testidx", DT_INT, 2));
      TEST_CHECK(openBtree(&tree, "testidx"));

      // insert keys
      for(i = 0; i < numInserts; i++)
	TEST_CHECK(insertKey(tree, keys[permute[i]], insert[permute[i]]));

      // check index stats
      TEST_CHECK(getNumEntries(tree, &testint));
      ASSERT_EQUALS_INT(testint, numInserts, "number of entries in btree");
      
      // execute scan, we should see tuples in sort order
      openTreeScan(tree, &sc);
      i = 0;
      while((rc = nextEntry(sc, &rid)) == RC_OK)
	{
	  RID expRid = insert[i++];
	  ASSERT_EQUALS_RID(expRid, rid, "did we find the correct RID?");
	}
      ASSERT_EQUALS_INT(RC_IM_NO_MORE_ENTRIES, rc, "no error returned by scan");
      ASSERT_EQUALS_INT(numInserts, i, "have seen all entries");
      closeTreeScan(sc);

      // cleanup
      TEST_CHECK(closeBtree(tree));
      TEST_CHECK(deleteBtree("testidx"));
      free(permute);
    }

  TEST_CHECK(shutdownIndexManager());
  freeValues(keys, numInserts);

  TEST_DONE();
}
// ************************************************************ 
void
testDelete (void)
{
  RID insert[] = { 
    {1,1},
    {2,3},
    {1,2},
    {3,5},
    {4,4},
    {3,2}, 
  };
  int numInserts = 6;
  Value **keys;
  char *stringKeys[] = {
    "i1",
    "i11",
    "i13",
    "i17",
    "i23",
    "i52"
  };
  testName = "test b-tree inserting and search";
  int i, iter;
  BTreeHandle *tree = NULL;
  int numDeletes = 3;
  bool *deletes = (bool *) malloc(numInserts * sizeof(bool));
  
  keys = createValues(stringKeys, numInserts);

  // init
  TEST_CHECK(initIndexManager(NULL));

  // create test b-tree and randomly remove entries
  for(iter = 0; iter < 50; iter++)
    {
      // randomly select entries for deletion (may select the same on twice)
      for(i = 0; i < numInserts; i++)
	deletes[i] = FALSE;
      for(i = 0; i < numDeletes; i++)
	deletes[rand() % numInserts] = TRUE;

      // init B-tree
      TEST_CHECK(createBtree("testidx", DT_INT, 2));
      TEST_CHECK(openBtree(&tree, "testidx"));

      // insert keys
      for(i = 0; i < numInserts; i++)
	TEST_CHECK(insertKey(tree, keys[i], insert[i]));
      
      // delete entries
      for(i = 0; i < numInserts; i++)
	{
	  if (deletes[i])
	    TEST_CHECK(deleteKey(tree, keys[i]));
	}

      // search for keys
      for(i = 0; i < 1000; i++)
	{
	  int pos = rand() % numInserts;
	  RID rid;
	  Value *key = keys[pos];
	  
	  if (deletes[pos])
	    {
	      int rc = findKey(tree, key, &rid);
	      ASSERT_TRUE((rc == RC_IM_KEY_NOT_FOUND), "entry was deleted, should not find it");
	    }
	  else
	    {
	      TEST_CHECK(findKey(tree, key, &rid));
	      ASSERT_EQUALS_RID(insert[pos], rid, "did we find the correct RID?");
	    }
	}

      // cleanup
      TEST_CHECK(closeBtree(tree));
      TEST_CHECK(deleteBtree("testidx"));
    }

  TEST_CHECK(shutdownIndexManager());
  freeValues(keys, numInserts);
  free(deletes);

  TEST_DONE();
}