Skip to content

Instantly share code, notes, and snippets.

@qxcv
Last active August 29, 2015 14:00
Show Gist options
  • Save qxcv/11204628 to your computer and use it in GitHub Desktop.
Save qxcv/11204628 to your computer and use it in GitHub Desktop.
comp2300 a2 automatic tester
#include <cstdio>
#include <random>
const int screen_width = 192;
const int screen_height = 160;
using namespace std;
random_device dev;
default_random_engine engine(dev());
void genpoint() {
uniform_int_distribution<int> xdist(0, screen_width - 1);
uniform_int_distribution<int> ydist(0, screen_height - 1);
printf("%02x%02x", xdist(engine), ydist(engine));
}
void genpwh() {
uniform_int_distribution<int> xdist(0, screen_width - 1);
uniform_int_distribution<int> ydist(0, screen_height - 1);
int x = xdist(engine);
int y = ydist(engine);
printf("%02x%02x", x, y);
uniform_int_distribution<int> wdist(1, screen_width - x);
uniform_int_distribution<int> hdist(1, screen_height - y);
int w = wdist(engine);
int h = hdist(engine);
printf("%02x%02x", w, h);
}
int main(int argc, char **argv) {
int num_operations = 10;
vector<char> operations;
operations.push_back('l');
operations.push_back('p');
operations.push_back('r');
uniform_int_distribution<int> operdist(0, operations.size() - 1);
for (int i = 0; i < num_operations; i++) {
char oper = operations[operdist(engine)];
putc(oper, stdout);
switch (oper) {
case 'l':
genpoint();
genpoint();
break;
case 'p':
genpoint();
break;
case 'r':
genpwh();
break;
default:
break;
}
}
printf("h");
return 0;
}
#!/bin/bash
# Automatically tests your implementation of the rPeANUt drawing language for
# bugs. Just pass this script the path to your implementation file and it will
# do the rest of the work for you :-)
thisdir="`dirname $0`"
implementation="$1"
if [ -f "$1" ]; then
true # fine, the file exists
else
echo "USAGE: $0 <path/to/your/drawing/program.s>"
exit 1
fi
cfname="`tempfile`"
dfname="`tempfile`"
i=0
while [ true ]; do
echo "Running test #$i"
"$thisdir/autogen" > "$cfname"
if [ $? -ne 0 ]; then
echo "Autogen failed. Oh my."
rm -f "$cfname" "$dfname"
exit 1
fi
"$thisdir/reference" < "$cfname" > "$dfname"
if [ $? -ne 0 ]; then
echo "Oops, looks like reference could not run autogen\'s output!"
echo -e "Here is the offending program:\\n"
cat "$cfname"
echo
rm -f "$cfname" "$dfname"
exit 1
fi
java -jar "$thisdir/rPeANUt2.3.jar" "$implementation" -dump < "$cfname" | diff - "$dfname"
if [ $? -eq 0 ]; then
echo "Success!"
else
echo -e "Failure :(\\nHere is the offending program:\\n"
cat "$cfname"
echo
rm -f "$cfname" "$dfname"
exit 1
fi
i=$(expr $i + 1)
done
rm -f "$cfname" "$dfname"
exit $rv
CXXFLAGS=-std=gnu++11 -Os -Wall -Wextra
URL=http://cs.anu.edu.au/courses/COMP2300/rpeanut/rPeANUt2.3.jar
.PHONY: all
all: autogen reference rPeANUt2.3.jar
chmod +x autotest
@echo "Build complete"
autogen: autogen.cpp
$(CXX) $(CXXFLAGS) autogen.cpp -o autogen
reference: reference.cpp
$(CXX) $(CXXFLAGS) reference.cpp -o reference
rPeANUt2.3.jar:
@echo "Fetching rPeANUt from $(URL)"
wget "$(URL)"
.PHONY: clean
clean:
@echo Not deleting rPeANUt2.3.jar. If you suspect that it is corrupted,
@echo or if it is not present, you should download a new copy from
@echo "$(URL)"
rm -f autogen reference
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <stdexcept>
#include <string>
using namespace std;
const int screen_width = 192;
const int screen_height = 160;
class Screen {
public:
Screen() {
clear();
}
~Screen() {
}
uint32_t framebuffer[screen_height][screen_width / 32];
void clear() {
for (int i = 0; i < screen_width / 32; i++) {
for (int j = 0; j < screen_height; j++) {
framebuffer[j][i] = 0;
}
}
}
void fill() {
rectangle(0, 0, screen_width, screen_height);
}
void point(int x, int y) {
if (x < 0 || x >= screen_width) {
throw runtime_error("x coordinate out of bounds\n");
}
if (y < 0 || y >= screen_height) {
throw runtime_error("y coordinate out of bounds\n");
}
framebuffer[y][x / 32] |= 1 << (x % 32);
}
void rectangle(int x, int y, int w, int h) {
for (int voffset = y; voffset < y + h; voffset++) {
for (int hoffset = x; hoffset < x + w; hoffset++) {
point(hoffset, voffset);
}
}
}
void line(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = x0 < x1 ? 1 : -1;
int sy = y0 < y1 ? 1 : -1;
int err = dx - dy;
while (true) {
point(x0, y0);
if (x0 == x1 && y0 == y1) break;
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}
}
void display() {
for (int j = 0; j < screen_height; j++) {
printf("%i", j);
for (int i = 0; i < screen_width / 32; i++) {
printf(":%08x", framebuffer[j][i]);
}
printf("\n");
}
}
};
int main(int argc, char** argv) {
int rv = 0;
Screen screen;
while (true) {
int instr = getc(stdin);
int x, y, x0, y0, x1, y1, w, h;
if (instr == EOF) {
fprintf(stderr, "Error: encountered EOF before halt.\n");
rv = 1;
break;
}
if (instr == 'h') {
break;
}
switch ((char)instr) {
case 'c':
screen.clear();
break;
case 'f':
screen.fill();
break;
case 'p':
if (scanf("%02x", &x) != 1) {
fprintf(stderr, "Error: point missing x argument\n");
rv = 1;
break;
}
if (scanf("%02x", &y) != 1) {
fprintf(stderr, "Error: point missing y argument\n");
rv = 1;
break;
}
screen.point(x, y);
break;
case 'r':
if (scanf("%02x", &x) != 1) {
fprintf(stderr, "Error: rectangle missing x argument\n");
rv = 1;
break;
}
if (scanf("%02x", &y) != 1) {
fprintf(stderr, "Error: rectangle missing y argument\n");
rv = 1;
break;
}
if (scanf("%02x", &w) != 1) {
fprintf(stderr, "Error: rectangle missing w argument\n");
rv = 1;
break;
}
if (scanf("%02x", &h) != 1) {
fprintf(stderr, "Error: rectangle missing h argument\n");
rv = 1;
break;
}
screen.rectangle(x, y, w, h);
break;
case 'l':
if (scanf("%02x", &x0) != 1) {
fprintf(stderr, "Error: line missing x0 argument\n");
rv = 1;
break;
}
if (scanf("%02x", &y0) != 1) {
fprintf(stderr, "Error: line missing y0 argument\n");
rv = 1;
break;
}
if (scanf("%02x", &x1) != 1) {
fprintf(stderr, "Error: line missing x1 argument\n");
rv = 1;
break;
}
if (scanf("%02x", &y1) != 1) {
fprintf(stderr, "Error: line missing y1 argument\n");
rv = 1;
break;
}
screen.line(x0, y0, x1, y1);
break;
default:
fprintf(stderr, "Error: unrecognised instruction: %c\n",
instr);
rv = 1;
}
if (rv == 1) break;
}
if (rv != 1) screen.display();
return rv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment