-
-
Save shayanjm/d4db6a3e83836343ce4a to your computer and use it in GitHub Desktop.
Weird C++ stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <chrono> | |
#include "Set.h" | |
unsigned time(); | |
int const number_of_tests = 1000; | |
int const maximum_set_size = 100; | |
int main (void) { | |
equalityTests(); | |
printf("The tests are over\n"); | |
} | |
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)); //errors here | |
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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assertion failed: (!isEqualToSet(&s, &t)), function equalityTests, file main.cpp, line 90. | |
[1] 3595 abort ./proj5 | |
============= | |
GDB OUTPUT AFTER SETTING BREAKPOINT AT LINE IN QUESTION (LINE 29 IN main.cpp) | |
============= | |
(gdb) call !isEqualToSet(&s, &t) | |
$3 = true | |
?? Why did the assertion fail? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include "Set.h" | |
// Some code goes here | |
bool isEqualToSet(const Set* self, const Set* other) { | |
if (self->len != other->len){ return false; } | |
for (int i = 0; i < self->len; ++i) { | |
if (self->elements[i] != other->elements[i]){ | |
return false; | |
} | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _Set_h | |
#define _Set_h 1 | |
struct Set { | |
int* elements; | |
int len; | |
int capacity; // NOTE: amortized doubling does not improve the time complexity for these problems | |
/* in fact, you can safely ignore capacity entirely. I'm including it here just in case some | |
* students feel more comfortable tracking the array size and the number of elements independently | |
* REPEATNG: You do not need to to implement amortized doubling in order to achieve the time-complexity | |
* bounds for this problem. You can achieve all the time complexity bounds even when | |
* capacity is ignored (i.e., your design can assume capacity == len). | |
*/ | |
}; | |
/* creation, assignment and destruction functions */ | |
void createSingletonSet(Set* self, int x); // construct a set containing only the element x | |
void createEmptySet(Set* self); // construct a set containing no elements | |
void createCopySet(Set* self, const Set* other); // construct a new set that has the same elements as other | |
void assignSet(Set* self, const Set* other); // replace the elements in this set with the elements of other | |
void destroySet(Set* self); | |
/* predicate functions */ | |
bool isMemberSet(const Set* self, int x); // return true iff x is an element in the set | |
bool isEmptySet(const Set* self); // return true iff the set contains zero elements | |
bool isEqualToSet(const Set* self, const Set* other); // return true iff this and other | |
// contain the same elements | |
bool isSubsetOf(const Set* self, const Set* other); // return true if all of the elements | |
// self are also elements of other | |
// NOTE: for any set, x, x is a subset | |
// of itself. | |
/* printing */ | |
void displaySet(const Set* self); | |
/* scalar manipulation functions */ | |
void insertSet(Set* self, int x); // insert x into this set. does nothing if | |
// x is already in the set | |
void removeSet(Set* self, int x); // remove x from this set. does nothing if | |
// x is not in the set | |
/* manipulations with sets */ | |
void unionInSet(Set* self, const Set* other); // replace the elements of self with self union other | |
void intersectFromSet(Set* self, const Set* other); // replace the elements of self with self intersection other | |
void subtractFromSet(Set* self, const Set* other); // replace the elements of self with self minus other | |
#endif /* _Set_h */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment