diff --git a/src/objects/o_float.c b/src/objects/o_float.c index 5dfffc4..0a95e96 100644 --- a/src/objects/o_float.c +++ b/src/objects/o_float.c @@ -3,6 +3,7 @@ static double to_float(Object *obj, Error *err); static void print(Object *obj, FILE *fp); +static _Bool op_eql(Object *self, Object *other); typedef struct { Object base; @@ -16,8 +17,24 @@ static TypeObject t_float = { .atomic = ATMTP_FLOAT, .to_float = to_float, .print = print, + .op_eql = op_eql }; +static _Bool op_eql(Object *self, Object *other) +{ + assert(self != NULL); + assert(self->type == &t_float); + assert(other != NULL); + assert(other->type == &t_float); + + FloatObject *i1, *i2; + + i1 = (FloatObject*) self; + i2 = (FloatObject*) other; + + return i1->val == i2->val; +} + static double to_float(Object *obj, Error *err) { assert(obj); diff --git a/tests/test.noja b/tests/test.noja new file mode 100644 index 0000000..1bf5b4a --- /dev/null +++ b/tests/test.noja @@ -0,0 +1,47 @@ + +# Test the equal operator +{ + assert(1 == 1); + assert(1.0 == 1.0); + assert('--' == '--'); +} + +# Test assignment. +{ + a = 3; + assert(a == 3); +} + +# Test operations on integers. +{ + # assert(4 == +4); -------------------- FAILING + # assert(0-4 == -4); ------------------ FAILING + assert(1 + 1 == 2); + assert(2 * 3 == 6); + assert(6 / 3 == 2); + assert(4 < 5); + assert(4 > 3); + assert(4 <= 5); + assert(4 <= 4); + assert(4 >= 3); + assert(4 == 4); + assert(4 != 3); +} + +# Test operations on floats. +{ + # assert(4.0 == +4.0); # -------------------- FAILING + # assert(0-4.0 == -4.0); # ------------------ FAILING + assert(1.0 + 1.0 == 2.0); + assert(2.0 * 3.0 == 6.0); + assert(6.0 / 3.0 == 2.0); + assert(4.0 < 5.0); + assert(4.0 > 3.0); + assert(4.0 <= 5.0); + assert(4.0 <= 4.0); + assert(4.0 >= 3.0); + assert(4.0 == 4.0); + assert(4.0 != 3.0); +} + +print('No assertion failed.\n'); \ No newline at end of file