/* * don't forget: it is OK to try to remove an element * that is NOT in the set. * If 'x' is not in the set 'self', then * removeSet should do nothing (it's not an error) * Otherwise, ('x' IS in the set), remove x. Be sure to update self->length * It is not necessary (nor recommended) to call malloc -- if removing an element means the * array on the heap is "too big", that's almost certainly OK, and reallocating a smaller array * is almost definitely NOT worth the trouble */ void removeSet(Set* self, int x) { if (self->len == 0) { return; } //can not remove anything to empty set Set copy_set; createCopySet (©_set, self); destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //recreate self with same capacity /*code here*/ int self_index = 0; int copy_index = 0; while (copy_index < copy_set.len) { if (x != copy_set.elements[copy_index]) { self->elements[self_index] = copy_set.elements[copy_index]; self_index += 1; } copy_index += 1; } self->len = self_index; self->capacity = copy_set.capacity; destroySet (©_set); //assert (checkValidSet (self)); //test code, remove later return; }
void visualTests() { Set s; Set t; int i; createEmptySet(&s); showOutput("The set constructed with the default constructor: ", &s); for (i = 0; i < 10; i += 1) { insertSet(&s, i); } showOutput("The set should be {0, ..., 9}: ", &s); // test Insert() and Contains() with '<<' for (i = 0; i < 10; i += 1) { insertSet(&s, i); } showOutput("The set should be {0, ..., 9}: ", &s); createCopySet(&t, &s); showOutput("The copy of s constructed with the copy constructor = ", &t); randomSet(&t); showOutput("The random set generated equals = ", &t); printf("The visual tests are over\n"); destroySet(&s); destroySet(&t); }
TEST(Test02, EqualityTests) { { for (int i = 0; i < NUM_TESTS; i++) { Set s; Set t; createEmptySet(&s); randomSet(&s); createCopySet(&t, &s); ASSERT_TRUE(isEqualToSet(&t, &s)); ASSERT_TRUE(isEqualToSet(&s, &t)); insertSet(&t, MAX_SET_SIZE); ASSERT_FALSE(isEqualToSet(&s, &t)); ASSERT_FALSE(isEqualToSet(&t, &s)); randomSet(&t); ASSERT_FALSE(isEqualToSet(&t, &s)); destroySet(&s); destroySet(&t); } } }
TEST(Test03, RelationalTests) { { Set s; Set t; createEmptySet(&s); createEmptySet(&t); for (int i = 0; i < NUM_TESTS; i++) { randomSet(&s); reassignSet(&t, &s); ASSERT_TRUE(isSubsetOf(&s, &t)); ASSERT_TRUE(isSubsetOf(&t, &s)); ASSERT_TRUE(isEqualToSet(&s, &t)); ASSERT_TRUE(isEqualToSet(&t, &s)); insertSet(&s, rand() % MAX_SET_SIZE + MAX_SET_SIZE); ASSERT_TRUE(isSubsetOf(&t, &s)); ASSERT_FALSE(isSubsetOf(&s, &t)); } destroySet(&s); destroySet(&t); } }
/* * add x as a new member to this set. * If x is already a member, then self should not be changed * Be sure to restore the design invariant property that elemnts[] remains sorted * (yes, you can assume it is sorted when the function is called, that's what an invariant is all about) */ void insertSet(Set* self, int x) { if (self->elements == 0) { //given empty set case createSingletonSet (self, x); return; } Set copy_set; createCopySet (©_set, self); copy_set.capacity += 1; //copy set with one more capacity destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //recreate self with larger capacity if (x < copy_set.elements[0]) { //if x is inserted at beginning self->elements[0] = x; for (int i = 0; i < copy_set.len; i += 1) { self->elements[i + 1] = copy_set.elements[i]; } self->len += 1; } else if (x > copy_set.elements[copy_set.len - 1]) { //if x is inserted at end for (int k = 0; k < copy_set.len; k += 1) { self->elements[k] = copy_set.elements[k]; } self->elements[self->len] = x; self->len += 1; } else { //if x is inserted in middle int copy_index = 0; int self_index = 0; while (copy_index < copy_set.len) { self->elements[self_index] = copy_set.elements[copy_index]; if (x < self->elements[self_index] && x > self->elements[self_index - 1]) { self->elements[self_index] = x; } else { copy_index += 1; } self_index += 1; } self->len = self_index; } self->capacity = copy_set.capacity; destroySet (©_set); //assert (checkValidSet (self)); //test code, remove later return; }
/* add all elements of other to self (obviously, without creating duplicate elements) */ void unionInSet(Set* self, const Set* other) { if (isEmptySet(other)) { return; } //adding nothing to self set if (isEqualToSet (self, other)) { return; } //same set, nothing to add if (isSubsetOf (other, self)) { return; } //everything in other already in self if (self == other) { return; } //same set, nothing to add if (self->len == 0) { createCopySet (self, other); } else { Set copy_set; createCopySet (©_set, self); copy_set.capacity = copy_set.len + other->len; destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //self now has room to union two unique sets int copy_index = 0; int other_index = 0; int self_index = 0; while (copy_index < copy_set.len || other_index < other->len) { if (copy_set.elements[copy_index] == other->elements[other_index]) { self->elements[self_index] = copy_set.elements[copy_index]; other_index += 1; copy_index += 1; } else if ((other_index >= other->len || copy_set.elements[copy_index] < other->elements[other_index]) && copy_index < copy_set.len) { self->elements[self_index] = copy_set.elements[copy_index]; copy_index += 1; } else if ((copy_index >= copy_set.len || copy_set.elements[copy_index] > other->elements[other_index]) && other_index < other->len) { self->elements[self_index] = other->elements[other_index]; other_index += 1; } self_index += 1; } self->len = self_index; self->capacity = copy_set.capacity; destroySet (©_set); } //might have to recreate insert code/subset code here /*code here*/ //have two sets (one a copy of self), depending on which number is lower of the two sets at their respective indicies, put that one first into self //assert (checkValidSet (self)); //test code, remove later }
/*Free functions*/ void destroySet(int lines, Cache destroyMe, int assoc){ if(destroyMe->nextLevel!=NULL){ /*need to determine kind*/ if(strcmp(destroyMe->nextLevel->type, "direct")==0){ destroyDirect(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc); } if((strlen(destroyMe->nextLevel->type)>=7)&&(strncmp(destroyMe->nextLevel->type,"assoc:", 5)==0)){ // printf("I came here"); destroySet(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc); } if((strlen(destroyMe->nextLevel->type)==5)&& (strcmp(destroyMe->nextLevel->type,"assoc")==0)){ destroySet(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc); } } for (int i=0;i<lines;i++){ if((strlen(destroyMe->type)>=7)&&(strncmp(destroyMe->type,"assoc:", 5)==0)){ /*destroy dll for each individual set*/ if(destroyMe->blocks[i]->LRU!=NULL){ SLDestroy(destroyMe->blocks[i]->LRU); destroyMe->blocks[i]->LRU=NULL; } else{ destroyQueue(destroyMe->blocks[i]->FIFO); destroyMe->blocks[i]->FIFO=NULL; } /*destroy queue for each individual set*/ } free(destroyMe->blocks[i]); } /*destroy FA's fifo or lru*/ if((strcmp(destroyMe->type,"FA")==0)||((strlen(destroyMe->type)==5)&& (strcmp(destroyMe->type,"assoc")==0))){ /*only for FA*/ if(destroyMe->LRU!=NULL){ SLDestroy(destroyMe->LRU); destroyMe->LRU=NULL; } else{ destroyQueue(destroyMe->FIFO); destroyMe->FIFO=NULL; } } free(destroyMe->blocks); free(destroyMe); destroyMe=NULL; return; }
void checkCase(setFun fun, Set* s1, Set* s2, Set* expect) { Set res; createCopySet(&res, s1); (*fun)(&res, s2); assert(isEqualToSet(&res, expect)); destroySet(&res); }
/* * The structure of the output is: * * {} <default constructed set> * {0,1,...,9} <insert 0-9> * {0,1,...,9} <insert 0-9 (fail b/c repeat)> * {0,1,...,9} <copy constructed> * {0,2,3,...,95,99} <random set> */ TEST(Test01, VisualTests) { { std::set<int> basic, random; Set s; Set t; int i; createEmptySet(&s); assertSetsAreEqual(basic, &s); for (i = 0; i < 10; i++) { insertSet(&s, i); basic.insert(i); } assertSetsAreEqual(basic, &s); for (i = 0; i < 10; i++) { insertSet(&s, i); } assertSetsAreEqual(basic, &s); createCopySet(&t, &s); assertSetsAreEqual(basic, &t); Set temp; int n = rand() % MAX_SET_SIZE + 1; createEmptySet(&temp); for (int i = 0; i < n; i++) { int val = rand() % MAX_SET_SIZE; insertSet(&temp, val); random.insert(val); } reassignSet(&t, &temp); destroySet(&temp); assertSetsAreEqual(random, &t); destroySet(&s); destroySet(&t); } }
/** * Same thing as createCopySet, but the old int array pointed to by elements is freed. Don't do * anything if the sets are already equal (quicker). * * param self: pointer to a struct 'self' that will have an int member that points to the members * of the malloc'ed set. Old set is overwrited by the set pointer to by 'other,' assuming that the * sets aren't already equal. * param other: pointer to self 'other' that will have an int member that points to the members of * the malloc'ed set. The members of this struct will be copied to the int array in 'self.' * return: N/A */ void assignSet(Set* self, const Set* other) { if (self == other) { /* 'self' is already 'other' */ return; } destroySet(self); /* free pointer to old set in 'self' */ createCopySet(self, other); /* overwrite 'self' with 'other' */ }
bool checkCaseNew(setFun fun, Set *s1, Set *s2, Set *expect) { Set res; createCopySet(&res, s1); (*fun)(&res, s2); bool test_result = isEqualToSet(&res, expect); destroySet(&res); return test_result; }
void equalityTests(void) { int i; for (i = 0; i < number_of_tests; i += 1) { Set s; Set t; createEmptySet(&s); randomSet(&s); createCopySet(&t, &s); assert(isEqualToSet(&t, &s)); assert(isEqualToSet(&s, &t)); insertSet(&t, maximum_set_size); assert(! isEqualToSet(&s, &t)); assert(! isEqualToSet(&t, &s)); randomSet(&t); assert(! isEqualToSet(&t, &s)); destroySet(&s); destroySet(&t); } // This test could fail with small probability printf("The equality tests have been passed\n"); }
void randomSet(Set* s) { Set t; int n = rand() % maximum_set_size + 1; int i; createEmptySet(&t); for (i = 0; i < n; i += 1) { insertSet(&t, rand() % maximum_set_size); } assignSet(s, &t); destroySet(&t); }
void relationalTests() { Set s; Set t; int i; createEmptySet(&s); createEmptySet(&t); for (i = 0; i < number_of_tests; i += 1) { randomSet(&s); assignSet(&t, &s); assert(isSubsetOf(&s, &t)); assert(isSubsetOf(&t, &s)); assert(isEqualToSet(&s, &t)); assert(isEqualToSet(&t, &s)); insertSet(&s, rand() % maximum_set_size + maximum_set_size); assert(isSubsetOf(&t, &s)); assert(! isSubsetOf(&s, &t)); } printf("The relational tests have been passed\n"); destroySet(&s); destroySet(&t); }
/* remove all elements from self that are not also elements of other */ void intersectFromSet(Set* self, const Set* other) { if (isEmptySet(self)) { return; } //can't remove anything from empty set if (isEqualToSet (self, other)) { return; } //all elements aready in common if (isSubsetOf (self, other)) { return; } //everything in self is in other, nothing to remove if (self == other) { return; } //all elements already in common /*code here*/ //have two sets (one a copy of self), if they don't match increase other index until they do, if they match add to self and increase both indicies //might have to recreate remove code/subset code here Set copy_set; createCopySet (©_set, self); copy_set.capacity = copy_set.len; destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //copy original set into copy_set and rebuild self int copy_index = 0; int other_index = 0; int self_index = 0; while (copy_index < copy_set.len && other_index < other->len) { if (copy_set.elements[copy_index] < other->elements[other_index]) { copy_index += 1; } else if (copy_set.elements[copy_index] > other->elements[other_index]) { other_index += 1; } else if (copy_set.elements[copy_index] == other->elements[other_index]) { self->elements[self_index] = copy_set.elements[copy_index]; self_index += 1; copy_index += 1; other_index += 1; } } self->len = self_index; self->capacity = copy_set.capacity; destroySet (©_set); //assert (checkValidSet (self)); //test code, remove later }
void randomSet(Set *s) { Set t; int n = rand() % MAX_SET_SIZE + 1; createEmptySet(&t); for (int i = 0; i < n; i++) { insertSet(&t, rand() % MAX_SET_SIZE); } reassignSet(s, &t); destroySet(&t); }
void destroyDirect(int lines, Cache destroyMe, int assoc){ if(destroyMe->nextLevel!=NULL){ /*need to determine kind*/ if(strcmp(destroyMe->nextLevel->type, "direct")==0){ destroyDirect(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc); } if((strlen(destroyMe->nextLevel->type)>=7)&&(strncmp(destroyMe->nextLevel->type,"assoc:", 5)==0)){ // printf("I came here"); destroySet(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc); } if((strlen(destroyMe->nextLevel->type)==5)&& (strcmp(destroyMe->nextLevel->type,"assoc")==0)){ destroySet(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc); } } for (int i=0;i<lines;i++){ free(destroyMe->blocks[i]); } free(destroyMe->blocks); free(destroyMe); destroyMe=NULL; return; }
int main(int argc, char *argv[]) { FILE *fp; char buffer[BUFSIZ]; SET *odd; int words; /* Check usage and open the file. */ if (argc != 2) { fprintf(stderr, "usage: %s file1\n", argv[0]); exit(EXIT_FAILURE); } if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]); exit(EXIT_FAILURE); } /* Insert or delete words to compute their parity. */ words = 0; odd = createSet(MAX_SIZE); while (fscanf(fp, "%s", buffer) == 1) { words ++; if (hasElement(odd, buffer)) removeElement(odd, buffer); else addElement(odd, buffer); } printf("%d total words\n", words); printf("%d words occur an odd number of times\n", numElements(odd)); fclose(fp); destroySet(odd); exit(EXIT_SUCCESS); }
/* done for you already */ void assignSet(Set* self, const Set* other) { if (self == other) { return; } destroySet(self); createCopySet(self, other); }
TEST(Test07, AlgebraicTests) { { Set empty; Set universal; Set r; Set s; Set t; Set u; Set v; Set w; int i; createEmptySet(&empty); createEmptySet(&universal); for (i = 0; i < MAX_SET_SIZE; i++) { insertSet(&universal, i); } createEmptySet(&r); createEmptySet(&s); createEmptySet(&t); createEmptySet(&u); createEmptySet(&v); createEmptySet(&w); ASSERT_FALSE(isEqualToSet(&empty, &universal)); for (i = 0; i < NUM_TESTS; i++) { randomSet(&u); randomSet(&v); randomSet(&w); /* w * (u - v) == w * u - w * v */ assignSet(&s, &u); subtractFromSet(&s, &v); assignSet(&r, &w); intersectFromSet(&r, &s); assignSet(&t, &w); intersectFromSet(&t, &v); assignSet(&s, &w); intersectFromSet(&s, &u); subtractFromSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); /* u * (v + w) == (u * v) + (u * w) */ assignSet(&s, &v); unionInSet(&s, &w); assignSet(&r, &u); intersectFromSet(&r, &s); assignSet(&t, &u); intersectFromSet(&t, &w); assignSet(&s, &u); intersectFromSet(&s, &v); unionInSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); /* universal - (u * v) == (universal - u) + (universal - v) */ assignSet(&s, &u); intersectFromSet(&s, &v); assignSet(&r, &universal); subtractFromSet(&r, &s); assignSet(&t, &universal); subtractFromSet(&t, &v); assignSet(&s, &universal); subtractFromSet(&s, &u); unionInSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); /* universal - (u + v) == (universal - u) * (universal - v) */ assignSet(&s, &u); unionInSet(&s, &v); assignSet(&r, &universal); subtractFromSet(&r, &s); assignSet(&t, &universal); subtractFromSet(&t, &v); assignSet(&s, &universal); subtractFromSet(&s, &u); intersectFromSet(&s, &t); ASSERT_TRUE(isEqualToSet(&r, &s)); } destroySet(&empty); destroySet(&universal); destroySet(&r); destroySet(&s); destroySet(&t); destroySet(&u); destroySet(&v); destroySet(&w); } }
/* remove all elements from self that are also elements of other */ void subtractFromSet(Set* self, const Set* other) { if (isEmptySet(self)) { return; } //can't remove anything from empty set if (isEmptySet(other)) { return; } //nothing to remove from self if (isSubsetOf (self, other)) { createEmptySet (self); return; } /*code here*/ //have two sets (one a copy of self), //might have to recreate remove code/subset code here /*Set intersect_set; createCopySet (&intersect_set, self); intersect_set.capacity = intersect_set.len; intersectFromSet (&intersect_set, other); //create temporary set with intersection of self and other */ Set copy_set; createCopySet (©_set, self); copy_set.capacity = copy_set.len; destroySet (self); self->elements = (int*) malloc (copy_set.capacity * sizeof (int)); //copy original set into copy_set and rebuild self /*int intersect_index = 0; int self_index = 0; int copy_index = 0; while (copy_index < copy_set.len) { if (copy_set.elements[copy_index] == intersect_set.elements[intersect_index]) { intersect_index += 1; copy_index += 1; } else if (intersect_index >= intersect_set.len || copy_set.elements[copy_index] < intersect_set.elements[intersect_index]) { self->elements[self_index] = copy_set.elements[copy_index]; copy_index += 1; self_index += 1; } else if (copy_set.elements[copy_index] > intersect_set.elements[intersect_index]) { intersect_index += 1; } }*/ int copy_index = 0; int self_index = 0; int other_index = 0; while (copy_index < copy_set.len) { if (other_index >= other->len || copy_set.elements[copy_index] < other->elements[other_index]) { self->elements[self_index] = copy_set.elements[copy_index]; self_index += 1; copy_index += 1; } else if (copy_set.elements[copy_index] > other->elements[other_index]) { other_index += 1; } else { copy_index += 1; other_index += 1; } } self->len = self_index; self->capacity = copy_set.capacity; destroySet (©_set); /*destroySet (&intersect_set);*/ //assert (checkValidSet (self)); //test code, remove later }
void specialCaseTests(void) { Set empty; Set universal; int i; Set s; Set r; createEmptySet(&empty); createEmptySet(&universal); createEmptySet(&r); for (i = 0; i < maximum_set_size; i += 1) { insertSet(&universal, i); } checkCase(&subtractFromSet, &universal, &universal, &empty); checkCase(&unionInSet, &universal, &universal, &universal); checkCase(&intersectFromSet, &universal, &universal, &universal); checkCase(&intersectFromSet, &universal, &empty, &empty); checkCase(&intersectFromSet, &empty, &universal, &empty); checkCase(&unionInSet, &universal, &empty, &universal); checkCase(&unionInSet, &empty, &universal, &universal); checkCase(&unionInSet, &empty, &empty, &empty); checkCase(&subtractFromSet, &empty, &empty, &empty); checkCase(&intersectFromSet, &empty, &empty, &empty); createEmptySet(&s); assert(isEmptySet(&s)); for (i = 0; i < 10; i += 1) { insertSet(&s, i); } assert(s.len == 10); for (i = 0; i < 10; i += 1) { assert(isMemberSet(&s, i)); } for (i = 0; i < 10; i += 1) { removeSet(&s, i); removeSet(&s, i); assert(s.len == 9 - i); } assert(isEmptySet(&s)); for (i = 0; i < number_of_tests; i += 1) { randomSet(&s); assert(isSubsetOf(&empty, &s)); assert(!isSubsetOf(&s, &empty)); assert(isSubsetOf(&s, &universal)); assert(!isSubsetOf(&universal, &s)); checkCase(&intersectFromSet, &empty, &s, &empty); checkCase(&intersectFromSet, &s, &empty, &empty); checkCase(&intersectFromSet, &universal, &s, &s); checkCase(&intersectFromSet, &s, &universal, &s); checkCase(&unionInSet, &universal, &s, &universal); checkCase(&unionInSet, &s, &universal, &universal); checkCase(&subtractFromSet, &s, &empty, &s); assignSet(&r, &universal); subtractFromSet(&r, &s); // r = u - s; checkCase(&subtractFromSet, &universal, &r, &s); // (u - (u - s) == s) checkCase(&unionInSet, &s, &r, &universal); // s + (u - s) == u checkCase(&unionInSet, &r, &s, &universal); // (u - s) + s == u } printf("The special case tests have been passed\n"); destroySet(&empty); destroySet(&universal); destroySet(&s); destroySet(&r); }
void algebraicTests(void) { Set empty; Set universal; int i; Set s; Set t; Set u; Set v; Set w; createEmptySet(&empty); createEmptySet(&universal); for (i = 0; i < maximum_set_size; i += 1) { insertSet(&universal, i); } createEmptySet(&s); createEmptySet(&t); createEmptySet(&u); createEmptySet(&v); createEmptySet(&w); for (i = 0; i < number_of_tests; i += 1) { randomSet(&u); randomSet(&v); randomSet(&w); /* u + v == v + u */ assignSet(&s, &u); unionInSet(&s, &v); assignSet(&t, &v); unionInSet(&t, &u); assert(isEqualToSet(&s, &t)); /* u + (v + w) == (u + v) + w */ assignSet(&t, &v); unionInSet(&t, &w); assignSet(&s, &u); unionInSet(&s, &t); assignSet(&t, &u); unionInSet(&t, &v); unionInSet(&t, &w); assert(isEqualToSet(&s, &t)); /* u * v == v * u */ assignSet(&s, &u); intersectFromSet(&s, &v); assignSet(&t, &v); intersectFromSet(&t, &u); assert(isEqualToSet(&s, &t)); /* u * (v * w) == (u * v) * w */ assignSet(&t, &v); intersectFromSet(&t, &w); assignSet(&s, &u); intersectFromSet(&s, &t); assignSet(&t, &u); intersectFromSet(&t, &v); intersectFromSet(&t, &w); assert(isEqualToSet(&s, &t)); /* u - v == u - (u * v) */ assignSet(&s, &u); intersectFromSet(&s, &v); assignSet(&t, &u); subtractFromSet(&t, &s); assignSet(&s, &u); subtractFromSet(&s, &v); assert(isEqualToSet(&s, &t)); /* additional tests, not implemented assert(w * (u - v) == w * u - w * v); assert(u * (v + w) == (u * v) + (u * w)); assert(universal - (u * v) == (universal - u) + (universal - v)); assert(universal - (u + v) == (universal - u) * (universal - v)); */ } printf("The algebraic tests have been passed\n"); destroySet(&empty); destroySet(&universal); destroySet(&s); destroySet(&t); destroySet(&u); destroySet(&v); destroySet(&w); }
void unionTimeFun(void) { Set t; createCopySet(&t, setA); unionInSet(&t, setB); destroySet(&t); }
void destroyLRU(void) { fclose(fp); destroySet(lru->pages); free(lru); }
void intersectTimeFun(void) { Set t; createCopySet(&t, setA); intersectFromSet(&t, setB); destroySet(&t); }
int main (int argc, char *argv []) { SET *set; FILE *fp; char buffer [BUFSIZ]; int words; /* Check usage and open the first file. */ if (argc == 1 || argc > 3) { fprintf (stderr, "usage: %s file1 [file2]\n", argv [0]); exit (EXIT_FAILURE); } if ((fp = fopen (argv [1], "r")) == NULL) { fprintf (stderr, "%s: cannot open %s\n", argv [0], argv [1]); exit (EXIT_FAILURE); } /* Insert all words into the set. */ words = 0; if ((set = createSet (MAX_SIZE)) == NULL) { fprintf (stderr, "%s: failed to create set\n", argv [0]); exit (EXIT_FAILURE); } while (fscanf (fp, "%s", buffer) == 1) { words ++; if (!hasElement (set, buffer)) if (!insertElement (set, strdup (buffer))) fprintf (stderr, "set full\n"); } printf ("%d total words\n", words); printf ("%d unique words\n", numElements (set)); fclose (fp); /* Try to open the second file. */ if (argc == 3) { if ((fp = fopen (argv [2], "r")) == NULL) { fprintf (stderr, "%s: cannot open %s\n", argv [0], argv [1]); exit (EXIT_FAILURE); } /* Delete all words in the second file. */ while (fscanf (fp, "%s", buffer) == 1) deleteElement (set, buffer); printf ("%d remaining words\n", numElements (set)); } destroySet (set); exit (EXIT_SUCCESS); }
int main(int argc, char* argv[]) { bool add = false, create = false, destroy = false, erase = false, header = false; bool list = false, lvcreate = false, lvdestroy = false, lvlist = false, lvmodify = false, lvresize = false, lvsnap = false; bool modify = false, remove = false, spare = false, watch = false; char * setLevel = 0, * setName = 0; /* options descriptor */ static struct option longopts[] = { { "add", required_argument, 0, 'a' }, { "create", no_argument, 0, 'c' }, { "destroy", required_argument, 0, 'd' }, { "erase", no_argument, 0, 'e' }, { "header", no_argument, 0, 'h' }, { "list", no_argument, 0, 'l' }, { "modify", required_argument, 0, 'm' }, { "remove", required_argument, 0, 'r' }, { "spare", required_argument, 0, 's' }, { "watch", no_argument, 0, 'w' }, { "lvcreate", required_argument, 0, 'C' }, { "lvdestroy", required_argument, 0, 'D' }, { "lvlist", no_argument, 0, 'L' }, { "lvmodify", required_argument, 0, 'M' }, { "lvresize", required_argument, 0, 'R' }, { "lvsnap", required_argument, 0, 'S' }, { "auto-rebuild",required_argument, 0, 'A' }, { "block-size", required_argument, 0, 'B' }, { "extents", no_argument, 0, 'E' }, { "hint", required_argument, 0, 'H' }, { "level", required_argument, 0, 'V' }, { "name", required_argument, 0, 'N' }, { "quick-rebuild",required_argument, 0, 'Q' }, { "size", required_argument, 0, 'Z' }, { "timeout", required_argument, 0, 'T' }, { "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, '?' }, { 0, 0, 0, 0 } }; int ch; while ((ch = getopt_long(argc, argv, "a:cd:ehlm:r:s:wC:D:LM:R:S:A:B:EH:V:N:Q:Z:T:v?", longopts, NULL)) != -1) { switch(ch) { case 'a': add = true; setName = strdup(optarg); break; case 'c': create = true; break; case 'd': destroy = true; setName = strdup(optarg); break; case 'e': erase = true; break; case 'h': header = true; break; case 'l': list = true; break; case 'm': modify = true; setName = strdup(optarg); break; case 'r': remove = true; setName = strdup(optarg); break; case 's': spare = true; setName = strdup(optarg); break; case 'w': watch = true; break; case 'C': lvcreate = true; setName = strdup(optarg); break; case 'D': lvdestroy = true; setName = strdup(optarg); break; case 'L': lvlist = true; break; case 'M': lvmodify = true; setName = strdup(optarg); break; case 'R': lvresize = true; setName = strdup(optarg); break; case 'S': lvsnap = true; setName = strdup(optarg); break; case 'A': autoRebuild = ((optarg[0] == 'Y') || (optarg[0] == 'y')) ? AUTO_YES : AUTO_NO; break; case 'B': sscanf(optarg, "%lli", &blockSize); break; case 'E': extents = true; break; case 'H': hint = strdup(optarg); break; case 'V': setLevel = strdup(optarg); break; case 'N': nickname = strdup(optarg); break; case 'Q': quickRebuild = ((optarg[0] == 'Y') || (optarg[0] == 'y')) ? AUTO_YES : AUTO_NO; break; case 'Z': sscanf(optarg, "%lli", &volSize); break; case 'T': sscanf(optarg, "%lli", &timeout); break; case 'v': verbose = true; break; case 0: case '?': default: usage(); exit(0); } } argc -= optind; argv += optind; if (!add && !create && !destroy && !erase && !header && !list && !modify && !remove && !spare && !watch && !lvcreate && !lvdestroy && !lvlist && !lvmodify && !lvresize && !lvsnap) { usage(); exit(0); } if (list) { listRAIDSets(); exit(0); } if (lvlist) { listLogicalVolumes(NULL, argc, argv); exit(0); } if (geteuid()) { printf("ERROR: you must be super user for this operation.\n"); exit(1); } if (erase) { erasePartition(argc, argv); exit(0); }; if (header) { dumpHeader(argc, argv); exit(0); }; if (watch) { CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, // const void *observer callBack, CFSTR(kAppleRAIDNotificationSetDiscovered), NULL, // const void *object CFNotificationSuspensionBehaviorHold); CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, // const void *observer callBack, CFSTR(kAppleRAIDNotificationSetTerminated), NULL, // const void *object CFNotificationSuspensionBehaviorHold); CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, // const void *observer callBack, CFSTR(kAppleRAIDNotificationSetChanged), NULL, // const void *object CFNotificationSuspensionBehaviorHold); CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, // const void *observer callBack, CFSTR(kAppleLVMNotificationVolumeDiscovered), NULL, // const void *object CFNotificationSuspensionBehaviorHold); CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, // const void *observer callBack, CFSTR(kAppleLVMNotificationVolumeTerminated), NULL, // const void *object CFNotificationSuspensionBehaviorHold); CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, // const void *observer callBack, CFSTR(kAppleLVMNotificationVolumeChanged), NULL, // const void *object CFNotificationSuspensionBehaviorHold); // this will not fail if there is no raid controller, ie, if AppleRAID class is not instantiated in the kernel AppleRAIDEnableNotifications(); } if (add) addMember(setName, CFSTR(kAppleRAIDMembersKey), argc, argv); if (create) createSet(setLevel, nickname, argc, argv); if (destroy) destroySet(setName, argc, argv); if (modify) modifySet(setName, argc, argv); if (remove) removeMember(setName, argc, argv); if (spare) addMember(setName, CFSTR(kAppleRAIDSparesKey), argc, argv); if (lvcreate) createLogicalVolume(setName, setLevel, argc, argv); if (lvdestroy) destroyLogicalVolume(setName, argc, argv); if (lvmodify) modifyLogicalVolume(setName, argc, argv); if (lvresize) resizeLogicalVolume(setName, argc, argv); if (lvsnap) snapshotLogicalVolume(setName, setLevel, argc, argv); if (watch) { printf("watching...\n"); // Set up a signal handler so we can clean up when we're interrupted from the command line // Otherwise we stay in our run loop forever. sig_t oldHandler = signal(SIGINT, signalHandler); if (oldHandler == SIG_ERR) { printf("Could not establish new signal handler"); exit(1); } // Start the run loop. Now we'll receive notifications. // printf("Starting run loop.\n"); CFRunLoopRun(); printf("Unexpectedly back from CFRunLoopRun()!\n"); } return 0; }
void subtractTimeFun(void) { Set t; createCopySet(&t, setA); subtractFromSet(&t, setB); destroySet(&t); }
TEST(Test05, SpecialCaseTests) { { Set empty; Set universal; Set s; Set r; int i; createEmptySet(&empty); createEmptySet(&universal); createEmptySet(&r); for (i = 0; i < MAX_SET_SIZE; i++) { insertSet(&universal, i); } ASSERT_TRUE(checkCaseNew(&subtractFromSet, &universal, &universal, &empty)); ASSERT_TRUE(checkCaseNew(&unionInSet, &universal, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &universal, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &universal, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &empty, &universal, &empty)); ASSERT_TRUE(checkCaseNew(&unionInSet, &universal, &empty, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &empty, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &empty, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&subtractFromSet, &empty, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &empty, &empty, &empty)); createEmptySet(&s); ASSERT_TRUE(isEmptySet(&s)); for (i = 0; i < 10; i++) { insertSet(&s, i); } ASSERT_TRUE(s.len == 10); for (i = 0; i < 10; i++) { ASSERT_TRUE(isMemberSet(&s, i)); } for (i = 0; i < 10; i++) { removeSet(&s, i); removeSet(&s, i); ASSERT_TRUE(s.len == 9 - i); } ASSERT_TRUE(isEmptySet(&s)); for (i = 0; i < NUM_TESTS; i++) { randomSet(&s); ASSERT_TRUE(isSubsetOf(&empty, &s)); ASSERT_FALSE(isSubsetOf(&s, &empty)); ASSERT_TRUE(isSubsetOf(&s, &universal)); ASSERT_FALSE(isSubsetOf(&universal, &s)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &empty, &s, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &s, &empty, &empty)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &universal, &s, &s)); ASSERT_TRUE(checkCaseNew(&intersectFromSet, &s, &universal, &s)); ASSERT_TRUE(checkCaseNew(&unionInSet, &universal, &s, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &s, &universal, &universal)); ASSERT_TRUE(checkCaseNew(&subtractFromSet, &s, &empty, &s)); assignSet(&r, &universal); subtractFromSet(&r, &s); ASSERT_TRUE(checkCaseNew(&subtractFromSet, &universal, &r, &s)); ASSERT_TRUE(checkCaseNew(&unionInSet, &s, &r, &universal)); ASSERT_TRUE(checkCaseNew(&unionInSet, &r, &s, &universal)); } destroySet(&empty); destroySet(&universal); destroySet(&s); destroySet(&r); } }