2024-01-03 12:48:25 +01:00
|
|
|
#include "include/test.h"
|
|
|
|
|
2024-01-03 16:20:42 +01:00
|
|
|
#include "../src/include/execute.h"
|
|
|
|
#include "../src/include/utils.h"
|
2024-01-03 12:48:25 +01:00
|
|
|
|
|
|
|
EvalResult execute(string s) {
|
2024-01-03 16:20:42 +01:00
|
|
|
Memory memory;
|
|
|
|
return execute(split_string(s, '\n'), memory);
|
2024-01-03 12:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2024-01-03 17:51:27 +01:00
|
|
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute(R"(
|
|
|
|
double a(int b, double c, int d) {
|
|
|
|
return b+c+d;
|
|
|
|
}
|
|
|
|
)"))),
|
2024-01-03 12:48:25 +01:00
|
|
|
"Déclaration de fonction",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
2024-01-03 17:51:27 +01:00
|
|
|
_TEST_IS_EXCEPTION(execute(R"(
|
|
|
|
int a(int b) {
|
|
|
|
int c(int d) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
return c(b);
|
|
|
|
};
|
|
|
|
)"),
|
|
|
|
ErrorType::NestedFunction
|
|
|
|
),
|
2024-01-03 12:48:25 +01:00
|
|
|
"Fonctions imbriquées",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
2024-01-03 17:51:27 +01:00
|
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
|
|
int sum(int a, int b) {
|
|
|
|
return a+b;
|
|
|
|
}
|
|
|
|
sum(20, 7);
|
|
|
|
)")) == 27),
|
2024-01-03 12:48:25 +01:00
|
|
|
"Exécution de fonction",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
2024-01-03 17:51:27 +01:00
|
|
|
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
|
|
|
int recurse(int a) {
|
|
|
|
if (a < 27)
|
|
|
|
return recurse(a+1);
|
|
|
|
else
|
|
|
|
return a;
|
|
|
|
};
|
|
|
|
recurse(0);
|
|
|
|
)")) == 27),
|
2024-01-03 12:48:25 +01:00
|
|
|
"Fonction récursive",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2024-01-03 17:51:27 +01:00
|
|
|
_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
|
|
|
|
);
|
|
|
|
|
2024-01-03 12:48:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|