Compare commits

...

3 Commits

Author SHA1 Message Date
Kayne Ruse 7bf18a744c Tweaked bounds check 2022-11-12 11:39:32 +00:00
Kayne Ruse fa20763c07 Corrected negative timers 2022-11-12 11:30:24 +00:00
Kayne Ruse 774f3d9e83 Corrected error messages in the timer lib 2022-11-12 10:07:54 +00:00
4 changed files with 104 additions and 15 deletions
+58 -14
View File
@@ -5,21 +5,43 @@
#include <stdio.h> #include <stdio.h>
//GOD DAMN IT: https://stackoverflow.com/questions/15846762/timeval-subtract-explanation
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) {
//normallize
if (x->tv_usec > 999999) {
x->tv_sec += x->tv_usec / 1000000;
x->tv_usec %= 1000000;
}
if (y->tv_usec > 999999) {
y->tv_sec += y->tv_usec / 1000000;
y->tv_usec %= 1000000;
}
//calc
result->tv_sec = x->tv_sec - y->tv_sec;
if ((result->tv_usec = x->tv_usec - y->tv_usec) < 0) {
if (result->tv_sec != 0) { //only works far from 0
result->tv_usec += 1000000;
result->tv_sec--; // borrow
}
}
return result->tv_sec < 0 || (result->tv_sec == 0 && result->tv_usec < 0);
}
//god damn it //god damn it
static struct timeval* diff(struct timeval* lhs, struct timeval* rhs) { static struct timeval* diff(struct timeval* lhs, struct timeval* rhs) {
struct timeval* d = ALLOCATE(struct timeval, 1); struct timeval* d = ALLOCATE(struct timeval, 1);
d->tv_sec = rhs->tv_sec - lhs->tv_sec; //I gave up, copied from SO
d->tv_usec = rhs->tv_usec - lhs->tv_usec; timeval_subtract(d, rhs, lhs);
if (d->tv_usec < 0) {
d->tv_sec--;
d->tv_usec += 1000 * 1000;
}
return d; return d;
} }
//callbacks
static int nativeStartTimer(Interpreter* interpreter, LiteralArray* arguments) { static int nativeStartTimer(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments //no arguments
if (arguments->count != 0) { if (arguments->count != 0) {
@@ -60,6 +82,7 @@ static int nativeStopTimer(Interpreter* interpreter, LiteralArray* arguments) {
} }
if (!IS_OPAQUE(timeLiteral)) { if (!IS_OPAQUE(timeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to _stopTimer\n");
freeLiteral(timeLiteral); freeLiteral(timeLiteral);
return -1; return -1;
} }
@@ -100,6 +123,14 @@ static int nativeCreateTimer(Interpreter* interpreter, LiteralArray* arguments)
} }
if (!IS_INTEGER(secondLiteral) || !IS_INTEGER(microsecondLiteral)) { if (!IS_INTEGER(secondLiteral) || !IS_INTEGER(microsecondLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to createTimer\n");
freeLiteral(secondLiteral);
freeLiteral(microsecondLiteral);
return -1;
}
if (AS_INTEGER(microsecondLiteral) <= -1000 * 1000 || AS_INTEGER(microsecondLiteral) >= 1000 * 1000 || (AS_INTEGER(secondLiteral) != 0 && AS_INTEGER(microsecondLiteral) < 0) ) {
interpreter->errorOutput("Microseconds out of range in createTimer\n");
freeLiteral(secondLiteral); freeLiteral(secondLiteral);
freeLiteral(microsecondLiteral); freeLiteral(microsecondLiteral);
return -1; return -1;
@@ -124,7 +155,7 @@ static int nativeCreateTimer(Interpreter* interpreter, LiteralArray* arguments)
static int nativeGetTimerSeconds(Interpreter* interpreter, LiteralArray* arguments) { static int nativeGetTimerSeconds(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments //no arguments
if (arguments->count != 1) { if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments to getTimerSeconds\n"); interpreter->errorOutput("Incorrect number of arguments to _getTimerSeconds\n");
return -1; return -1;
} }
@@ -137,6 +168,7 @@ static int nativeGetTimerSeconds(Interpreter* interpreter, LiteralArray* argumen
} }
if (!IS_OPAQUE(timeLiteral)) { if (!IS_OPAQUE(timeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to _getTimerSeconds\n");
freeLiteral(timeLiteral); freeLiteral(timeLiteral);
return -1; return -1;
} }
@@ -157,7 +189,7 @@ static int nativeGetTimerSeconds(Interpreter* interpreter, LiteralArray* argumen
static int nativeGetTimerMicroseconds(Interpreter* interpreter, LiteralArray* arguments) { static int nativeGetTimerMicroseconds(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments //no arguments
if (arguments->count != 1) { if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments to getTimerSeconds\n"); interpreter->errorOutput("Incorrect number of arguments to _getTimerMicroseconds\n");
return -1; return -1;
} }
@@ -170,6 +202,7 @@ static int nativeGetTimerMicroseconds(Interpreter* interpreter, LiteralArray* ar
} }
if (!IS_OPAQUE(timeLiteral)) { if (!IS_OPAQUE(timeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to _getTimerMicroseconds\n");
freeLiteral(timeLiteral); freeLiteral(timeLiteral);
return -1; return -1;
} }
@@ -190,7 +223,7 @@ static int nativeGetTimerMicroseconds(Interpreter* interpreter, LiteralArray* ar
static int nativeCompareTimer(Interpreter* interpreter, LiteralArray* arguments) { static int nativeCompareTimer(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments //no arguments
if (arguments->count != 2) { if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments to _stopTimer\n"); interpreter->errorOutput("Incorrect number of arguments to _compareTimer\n");
return -1; return -1;
} }
@@ -209,6 +242,7 @@ static int nativeCompareTimer(Interpreter* interpreter, LiteralArray* arguments)
} }
if (!IS_OPAQUE(lhsLiteral) || !IS_OPAQUE(rhsLiteral)) { if (!IS_OPAQUE(lhsLiteral) || !IS_OPAQUE(rhsLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to _compareTimer\n");
freeLiteral(lhsLiteral); freeLiteral(lhsLiteral);
freeLiteral(rhsLiteral); freeLiteral(rhsLiteral);
return -1; return -1;
@@ -246,6 +280,7 @@ static int nativeTimerToString(Interpreter* interpreter, LiteralArray* arguments
} }
if (!IS_OPAQUE(timeLiteral)) { if (!IS_OPAQUE(timeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to _timerToString\n");
freeLiteral(timeLiteral); freeLiteral(timeLiteral);
return -1; return -1;
} }
@@ -253,9 +288,17 @@ static int nativeTimerToString(Interpreter* interpreter, LiteralArray* arguments
struct timeval* timer = AS_OPAQUE(timeLiteral); struct timeval* timer = AS_OPAQUE(timeLiteral);
//create the string literal //create the string literal
char buffer[128]; Literal resultLiteral = TO_NULL_LITERAL;
snprintf(buffer, 128, "%ld.%06ld", timer->tv_sec, timer->tv_usec); if (timer->tv_sec == 0 && timer->tv_usec < 0) { //special case, for when the negative sign is encoded in the usec
Literal resultLiteral = TO_STRING_LITERAL( copyString(buffer, strlen(buffer)), strlen(buffer)); char buffer[128];
snprintf(buffer, 128, "-%ld.%06ld", timer->tv_sec, -timer->tv_usec);
resultLiteral = TO_STRING_LITERAL( copyString(buffer, strlen(buffer)), strlen(buffer));
}
else { //normal case
char buffer[128];
snprintf(buffer, 128, "%ld.%06ld", timer->tv_sec, timer->tv_usec);
resultLiteral = TO_STRING_LITERAL( copyString(buffer, strlen(buffer)), strlen(buffer));
}
pushLiteralArray(&interpreter->stack, resultLiteral); pushLiteralArray(&interpreter->stack, resultLiteral);
@@ -269,7 +312,7 @@ static int nativeTimerToString(Interpreter* interpreter, LiteralArray* arguments
static int nativeDestroyTimer(Interpreter* interpreter, LiteralArray* arguments) { static int nativeDestroyTimer(Interpreter* interpreter, LiteralArray* arguments) {
//no arguments //no arguments
if (arguments->count != 1) { if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments to _desroyTimer\n"); interpreter->errorOutput("Incorrect number of arguments to _destroyTimer\n");
return -1; return -1;
} }
@@ -282,6 +325,7 @@ static int nativeDestroyTimer(Interpreter* interpreter, LiteralArray* arguments)
} }
if (!IS_OPAQUE(timeLiteral)) { if (!IS_OPAQUE(timeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to _destroyTimer\n");
freeLiteral(timeLiteral); freeLiteral(timeLiteral);
return -1; return -1;
} }
+13
View File
@@ -0,0 +1,13 @@
import timer;
var a = createTimer(1, 0);
var b = createTimer(2, 0);
print a.compareTimer(b).timerToString();
print b.compareTimer(a).timerToString();
var c = createTimer(0, 1);
var d = createTimer(0, 2);
print c.compareTimer(d).timerToString();
print d.compareTimer(c).timerToString();
+1 -1
View File
@@ -6,7 +6,7 @@
#define TOY_VERSION_MAJOR 0 #define TOY_VERSION_MAJOR 0
#define TOY_VERSION_MINOR 6 #define TOY_VERSION_MINOR 6
#define TOY_VERSION_PATCH 1 #define TOY_VERSION_PATCH 2
#define TOY_VERSION_BUILD __DATE__ " " __TIME__ #define TOY_VERSION_BUILD __DATE__ " " __TIME__
//platform exports/imports //platform exports/imports
+32
View File
@@ -56,5 +56,37 @@
timer.destroyTimer(); timer.destroyTimer();
} }
{
//test positive and negative values of timers
import timer;
var a = createTimer(1, 0);
var b = createTimer(2, 0);
var acmp = a.compareTimer(b);
var bcmp = b.compareTimer(a);
var c = createTimer(0, 1);
var d = createTimer(0, 2);
var ccmp = c.compareTimer(d);
var dcmp = d.compareTimer(c);
assert acmp.timerToString() == "1.000000", "positive and negative tests failed (acmp)";
assert bcmp.timerToString() == "-1.000000", "positive and negative tests failed (bcmp)";
assert ccmp.timerToString() == "0.000001", "positive and negative tests failed (ccmp)";
assert dcmp.timerToString() == "-0.000001", "positive and negative tests failed (dcmp)";
a.destroyTimer();
b.destroyTimer();
c.destroyTimer();
d.destroyTimer();
acmp.destroyTimer();
bcmp.destroyTimer();
ccmp.destroyTimer();
dcmp.destroyTimer();
}
print "All good"; print "All good";