Created
February 26, 2015 02:10
-
-
Save bantmen/3a0bbb7b55b76900e99e to your computer and use it in GitHub Desktop.
A2 mytest
This file contains hidden or 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 <unistd.h> | |
| #include <sys/types.h> | |
| #include <sys/mman.h> | |
| #include "smalloc.h" | |
| #define SIZE 4096 * 64 | |
| /* | |
| Stress testing, will have enough memory if sfree is working. | |
| Total requested size is greater than SIZE. | |
| Testing smalloc and sfree at the same time. | |
| */ | |
| int stress_test() { | |
| mem_init(SIZE); | |
| char *ptrs[10]; | |
| int i, num_bytes; | |
| for(i=0; i<10; i++) { | |
| num_bytes = (i+1) * 20000; | |
| printf("Writing to mem %d\n", i); | |
| ptrs[i] = smalloc(num_bytes); | |
| write_to_mem(num_bytes, ptrs[i], i); | |
| printf("Freeing %p result = %d\n", ptrs[i], sfree(ptrs[i])); | |
| } | |
| mem_clean(); | |
| return 0; | |
| } | |
| /* | |
| Will exceed the memory and check whether smalloc returns NULL. | |
| Also, completely empties the freelist. | |
| Mainly testing for smalloc. | |
| */ | |
| int excessive_smalloc() { | |
| mem_init(SIZE); | |
| char *ptrs[3]; | |
| int i; | |
| int num_bytes = SIZE/2; | |
| for(i=0; i<3; i++) { | |
| printf("Writing to mem %d with size %d\n", i, num_bytes); | |
| ptrs[i] = smalloc(num_bytes); | |
| if (ptrs[i] != NULL) { // See whether we had enough memory for smalloc | |
| write_to_mem(num_bytes, ptrs[i], i); | |
| } | |
| } | |
| printf("Freeing %p result = %d\n", ptrs[0], sfree(ptrs[0])); | |
| print_allocated(); | |
| print_free(); | |
| mem_clean(); | |
| return 0; | |
| } | |
| /* | |
| Call all the test cases. | |
| */ | |
| int main(void) { | |
| // printf("Doing stress test\n"); | |
| // stress_test(); | |
| printf("Now, excessive smalloc test\n"); | |
| excessive_smalloc(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment