[python] Add matplotlib test WIP

This commit is contained in:
Émilie Feral
2020-04-01 11:32:21 +02:00
committed by LeaNumworks
parent 11ac25e935
commit 2f171eb2fd
5 changed files with 175 additions and 49 deletions

View File

@@ -1,21 +1,5 @@
#include <quiz.h>
#include <assert.h>
#include <string.h>
#include <python/port/port.h>
#include <apps/code/app.h>
class TestExecutionEnvironment : public MicroPython::ExecutionEnvironment {
public:
void printText(const char * text, size_t length) override;
};
void TestExecutionEnvironment::printText(const char * text, size_t length) {
quiz_print(text);
}
static constexpr int s_pythonHeapSize = Code::App::k_pythonHeapSize;
static char s_pythonHeap[s_pythonHeapSize];
#include "execution_environment.h"
static const char * s_mandelbrotScript = R"(#
def mandelbrot(N_iteration):
@@ -33,37 +17,6 @@ mandelbrot(2)
print('ok')
)";
// TODO: this will be obsolete when runCode will take a parameter to choose the input type
void inlineToBeSingleInput(char * buffer, size_t bufferSize, const char * script) {
static const char * openExec = "exec(\"";
static const char * closeExec = "\")";
assert(strlen(script) + strlen(openExec) + strlen(closeExec) < bufferSize);
char * bufferChar = buffer;
bufferChar += strlcpy(buffer, openExec, bufferSize);
const char * scriptChar = script;
while (*scriptChar != 0) {
assert(bufferChar - buffer + 2 < bufferSize - 1);
if (*scriptChar == '\n') {
// Turn carriage return in {'\', 'n'} to be processed by exec
*bufferChar++ = '\\';
*bufferChar++ = 'n';
} else {
*bufferChar++ = *scriptChar;
}
scriptChar++;
}
bufferChar += strlcpy(bufferChar, closeExec, buffer + bufferSize - bufferChar);
assert(bufferChar - buffer < bufferSize);
*bufferChar = 0;
}
QUIZ_CASE(python_mandelbrot) {
constexpr size_t bufferSize = 500;
char buffer[bufferSize];
inlineToBeSingleInput(buffer, bufferSize, s_mandelbrotScript);
MicroPython::init(s_pythonHeap, s_pythonHeap + s_pythonHeapSize);
TestExecutionEnvironment env;
env.runCode(buffer);
MicroPython::deinit();
assert_script_execution_succeeds(s_mandelbrotScript);
}