added tests and equal operator on floats

This commit is contained in:
cozis
2022-03-06 17:04:20 +01:00
parent 4394d95829
commit cb18c6ff91
2 changed files with 64 additions and 0 deletions
+17
View File
@@ -3,6 +3,7 @@
static double to_float(Object *obj, Error *err); static double to_float(Object *obj, Error *err);
static void print(Object *obj, FILE *fp); static void print(Object *obj, FILE *fp);
static _Bool op_eql(Object *self, Object *other);
typedef struct { typedef struct {
Object base; Object base;
@@ -16,8 +17,24 @@ static TypeObject t_float = {
.atomic = ATMTP_FLOAT, .atomic = ATMTP_FLOAT,
.to_float = to_float, .to_float = to_float,
.print = print, .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) static double to_float(Object *obj, Error *err)
{ {
assert(obj); assert(obj);
+47
View File
@@ -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');