added tests and equal operator on floats
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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');
|
||||
Reference in New Issue
Block a user