156 lines
3.6 KiB
C++
156 lines
3.6 KiB
C++
#include "include/test.hpp"
|
|
|
|
#include "../src/include/execute.hpp"
|
|
#include "../src/include/utils.hpp"
|
|
|
|
EvalResult execute(string s) {
|
|
Memory memory;
|
|
return execute(split_string(s, '\n'), memory);
|
|
}
|
|
|
|
int main() {
|
|
_TEST_PRESENTATION("Fonctions");
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("int a();"))),
|
|
"Prototype de fonction sans variables",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("void a(int b);"))),
|
|
"Prototype de fonction à une variable",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("double a(int b, double c, int d);"))),
|
|
"Prototype de fonction à plusieurs variables",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute(R"(
|
|
double a(int b, double c, int d) {
|
|
return b+c+d;
|
|
}
|
|
)"))),
|
|
"Déclaration de fonction",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_IS_EXCEPTION(execute(R"(
|
|
int a(int b) {
|
|
int c(int d) {
|
|
return d;
|
|
}
|
|
return c(b);
|
|
};
|
|
)"),
|
|
ErrorType::NestedFunction
|
|
),
|
|
"Fonctions imbriquées",
|
|
true
|
|
);
|
|
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
int sum(int a, int b) {
|
|
return a+b;
|
|
}
|
|
sum(20, 7);
|
|
)")) == 27),
|
|
"Exécution de fonction",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
int recurse(int a) {
|
|
if (a < 27)
|
|
return recurse(a+1);
|
|
else
|
|
return a;
|
|
};
|
|
recurse(0);
|
|
)")) == 27),
|
|
"Fonction récursive",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
int x(int a);
|
|
int y(int a) {
|
|
return x(a);
|
|
}
|
|
int x(int a) {
|
|
if (a < 75) {
|
|
return y(a+1);
|
|
}
|
|
return a;
|
|
}
|
|
x(0);
|
|
)")) == 75),
|
|
"Fonctions mutuellement récursives",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_IS_EXCEPTION(execute("int f(int x, void y);"), ErrorType::IncompleteType),
|
|
"Argument void (prototype)",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_IS_EXCEPTION(execute("int f(int x, void y) { return x+2; };"), ErrorType::IncompleteType),
|
|
"Argument void (déclaration)",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
int x = 1;
|
|
int a() {
|
|
return x;
|
|
}
|
|
int b() {
|
|
int x = 2;
|
|
return a();
|
|
}
|
|
b();
|
|
)")) == 1),
|
|
"Closure capture",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
int x = 1;
|
|
int a() {
|
|
return x;
|
|
}
|
|
x = 2;
|
|
a();
|
|
)")) == 2),
|
|
"Closure mutation externe",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
int x = 1;
|
|
void a() {
|
|
x = 2;
|
|
}
|
|
a();
|
|
x;
|
|
)")) == 2),
|
|
"Closure mutation interne",
|
|
true
|
|
);
|
|
|
|
return 0;
|
|
} |