|
1 | 1 | #include <tests/doctest.h> |
2 | 2 |
|
3 | 3 | #include <cmath> |
| 4 | +#include <cstring> |
4 | 5 |
|
5 | 6 | #include <lpython/python_evaluator.h> |
6 | 7 | #include <libasr/codegen/evaluator.h> |
@@ -1270,3 +1271,90 @@ TEST_CASE("PythonCompiler u16 declaration") { |
1270 | 1271 | CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); |
1271 | 1272 | CHECK(r.result.u32 == 45); |
1272 | 1273 | } |
| 1274 | + |
| 1275 | +TEST_CASE("PythonCompiler string 1") { |
| 1276 | + CompilerOptions cu; |
| 1277 | + cu.po.disable_main = true; |
| 1278 | + cu.emit_debug_line_column = false; |
| 1279 | + cu.generate_object_code = false; |
| 1280 | + cu.interactive = true; |
| 1281 | + cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); |
| 1282 | + PythonCompiler e(cu); |
| 1283 | + LCompilers::Result<PythonCompiler::EvalResult> |
| 1284 | + |
| 1285 | + r = e.evaluate2("\"My String\""); |
| 1286 | + CHECK(r.ok); |
| 1287 | + CHECK(r.result.type == PythonCompiler::EvalResult::string); |
| 1288 | + CHECK(std::strcmp(r.result.str, "My String") == 0); |
| 1289 | + |
| 1290 | + r = e.evaluate2("\"s1\" + \" \" + \"s2\""); |
| 1291 | + CHECK(r.ok); |
| 1292 | + CHECK(r.result.type == PythonCompiler::EvalResult::string); |
| 1293 | + CHECK(std::strcmp(r.result.str, "s1 s2") == 0); |
| 1294 | +} |
| 1295 | + |
| 1296 | +TEST_CASE("PythonCompiler string 2") { |
| 1297 | + CompilerOptions cu; |
| 1298 | + cu.po.disable_main = true; |
| 1299 | + cu.emit_debug_line_column = false; |
| 1300 | + cu.generate_object_code = false; |
| 1301 | + cu.interactive = true; |
| 1302 | + cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); |
| 1303 | + PythonCompiler e(cu); |
| 1304 | + LCompilers::Result<PythonCompiler::EvalResult> |
| 1305 | + |
| 1306 | + r = e.evaluate2("s: str"); |
| 1307 | + CHECK(r.ok); |
| 1308 | + CHECK(r.result.type == PythonCompiler::EvalResult::none); |
| 1309 | + |
| 1310 | + r = e.evaluate2("s"); |
| 1311 | + CHECK(r.ok); |
| 1312 | + CHECK(r.result.type == PythonCompiler::EvalResult::string); |
| 1313 | + CHECK(r.result.str == nullptr); |
| 1314 | + |
| 1315 | + r = e.evaluate2(R"( |
| 1316 | +s = "" |
| 1317 | +i: i32 = 0 |
| 1318 | +for i in range(10): |
| 1319 | + s += str(i) |
| 1320 | +)"); |
| 1321 | + CHECK(r.ok); |
| 1322 | + CHECK(r.result.type == PythonCompiler::EvalResult::statement); |
| 1323 | + |
| 1324 | + r = e.evaluate2("s"); |
| 1325 | + CHECK(r.ok); |
| 1326 | + CHECK(r.result.type == PythonCompiler::EvalResult::string); |
| 1327 | + CHECK(std::strcmp(r.result.str, "0123456789") == 0); |
| 1328 | +} |
| 1329 | + |
| 1330 | +TEST_CASE("PythonCompiler string 3") { |
| 1331 | + CompilerOptions cu; |
| 1332 | + cu.po.disable_main = true; |
| 1333 | + cu.emit_debug_line_column = false; |
| 1334 | + cu.generate_object_code = false; |
| 1335 | + cu.interactive = true; |
| 1336 | + cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); |
| 1337 | + PythonCompiler e(cu); |
| 1338 | + LCompilers::Result<PythonCompiler::EvalResult> |
| 1339 | + |
| 1340 | + r = e.evaluate2(R"( |
| 1341 | +def my_concat(x: str, y: str) -> str: |
| 1342 | + return x + " " + y |
| 1343 | +)"); |
| 1344 | + CHECK(r.ok); |
| 1345 | + CHECK(r.result.type == PythonCompiler::EvalResult::none); |
| 1346 | + |
| 1347 | + r = e.evaluate2("s: str = \"0123456789\""); |
| 1348 | + CHECK(r.ok); |
| 1349 | + CHECK(r.result.type == PythonCompiler::EvalResult::none); |
| 1350 | + |
| 1351 | + r = e.evaluate2("my_concat(s, \"NUM\")"); |
| 1352 | + CHECK(r.ok); |
| 1353 | + CHECK(r.result.type == PythonCompiler::EvalResult::string); |
| 1354 | + CHECK(std::strcmp(r.result.str, "0123456789 NUM") == 0); |
| 1355 | + |
| 1356 | + r = e.evaluate2("my_concat(\"Python\", \"REPL\")"); |
| 1357 | + CHECK(r.ok); |
| 1358 | + CHECK(r.result.type == PythonCompiler::EvalResult::string); |
| 1359 | + CHECK(std::strcmp(r.result.str, "Python REPL") == 0); |
| 1360 | +} |
0 commit comments