Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Created November 11, 2021 00:26
Show Gist options
  • Save ProfAvery/cc5611fbdc7750ae7fb7a3ce9a7945ce to your computer and use it in GitHub Desktop.
Save ProfAvery/cc5611fbdc7750ae7fb7a3ce9a7945ce to your computer and use it in GitHub Desktop.
CPSC 351 - Fall 2021 - Project 2 - Test Case 1
#include <cstdlib>
#include <cstddef>
#include <cassert>
#include <iostream>
#include "allocator.h"
using std::byte;
using std::cout;
using std::endl;
using std::flush;
void fill_block(byte *block, size_t size, char value)
{
for (unsigned int i = 0; i < size; i++)
{
block[i] = (byte)value;
}
}
void check_fill(byte *block, size_t size, char expected)
{
for (unsigned int i = 0; i < size; i++)
{
char c = (char)block[i];
assert(c == expected);
}
}
int main()
{
Allocator *a = new Allocator();
assert(a != NULL);
cout << "Creating Block A: " << flush;
byte *bA = a->malloc(2 * KB);
assert(bA != NULL);
fill_block(bA, 2 * KB, 'A');
check_fill(bA, 2 * KB, 'A');
cout << "OK" << endl;
a->dump();
cout << "Creating Block B: " << flush;
byte *bB = a->malloc(2 * KB);
assert(bB != NULL);
fill_block(bB, 2 * KB, 'B');
check_fill(bB, 2 * KB, 'B');
cout << "OK" << endl;
a->dump();
cout << "Failing to create Block C: " << flush;
byte *bC = a->malloc(4 * KB);
assert(bC == NULL);
cout << "OK" << endl;
cout << "Checking and freeing Block A: " << flush;
check_fill(bA, 2 * KB, 'A');
a->free(bA);
cout << "OK" << endl;
cout << "Checking and freeing Block B: " << flush;
check_fill(bB, 2 * KB, 'B');
a->free(bB);
cout << "OK" << endl;
cout << "Creating Block C: " << flush;
bC = a->malloc(4 * KB);
assert(bC != NULL);
fill_block(bC, 4 * KB, 'C');
check_fill(bC, 4 * KB, 'C');
cout << "OK" << endl;
a->dump();
delete a;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment