Skip to content

Commit

Permalink
unit test demo added
Browse files Browse the repository at this point in the history
  • Loading branch information
koder77 committed May 5, 2023
1 parent 1ecba4a commit 3dc666e
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 5 deletions.
5 changes: 4 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ L1VM (2.3.0)
BUGFIX: file/open did not set file open flag.
BUGFIXES: ciphersaber module now works!

-- Stefan Pietzonke <jay-t@gmx.net> Sun 30 Apr 2023 17:33 +0200
NEW: now the l1vm-build.sh script searches for includes in the current folder too!
ADDED a unit test demo.

-- Stefan Pietzonke <jay-t@gmx.net> Fri 05 May 2023 19:48 +0200

L1VM (2.2.0)
NEW: The Brackets compiler has now an object type for some OOP style functions.
Expand Down
4 changes: 3 additions & 1 deletion l1vm-build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
l1pre $1.l1com out.l1com ~/l1vm/include/
secondinclude=${PWD}
secondinclude+='/'
l1pre $1.l1com out.l1com ~/l1vm/include/ $secondinclude
RETVAL=$?
[ $RETVAL -ne 0 ] && echo "preprocessor build failed: " && echo $i && exit 1
l1com out $2 $3 $4 $5 $6
Expand Down
38 changes: 35 additions & 3 deletions prepro/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define COMMENT_COMP_SB "//"

U1 include_path[MAXSTRLEN + 1];
U1 include_path_two[MAXSTRLEN + 1];

struct define
{
Expand Down Expand Up @@ -635,8 +636,24 @@ S2 include_file (U1 *line_str)
fincludeptr = fopen ((const char *) include_full_path, "r");
if (fincludeptr == NULL)
{
printf ("ERROR: can't open include file: '%s' !\n", include_file_name);
return (1);
// try to load file with second include path
strcpy ((char *) include_full_path, (const char *) include_path_two);
include_path_len = strlen_safe ((const char *) include_full_path, MAXLINELEN);
include_name_len = strlen_safe ((const char *) include_file_name, MAXLINELEN);
if (include_path_len + include_name_len > MAXLINELEN)
{
printf ("ERROR: can't open include files, names too long!\n");
return (1);
}
// cat filename to path name
strcat ((char *) include_full_path, (const char *) include_file_name);

fincludeptr = fopen ((const char *) include_full_path, "r");
if (fincludeptr == NULL)
{
printf ("ERROR: can't open include file: '%s' !\n", include_file_name);
return (1);
}
}

// read include file
Expand Down Expand Up @@ -829,7 +846,8 @@ int main (int ac, char *av[])

char *read;
U1 ok;
S4 pos, slen;
S4 pos;
S4 slen;

if (ac < 4)
{
Expand Down Expand Up @@ -870,6 +888,20 @@ int main (int ac, char *av[])
exit (1);
}
strcpy ((char *) include_path, av[3]);
strcpy ((char *) include_path_two, "");

if (ac == 5)
{
if (strlen_safe ((const char *) av[4], MAXLINELEN) > MAXLINELEN - 1)
{
printf ("ERROR: include path: '%s' too long!\n", av[4]);
fclose (finptr);
fclose (foutptr);
exit (1);
}
strcpy ((char *) include_path_two, av[4]);
printf ("second include path: '%s'\n", include_path_two);
}

// read input line loop
ok = TRUE;
Expand Down
5 changes: 5 additions & 0 deletions unit-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UNIT TEST DEMO
==============

This shows how to do simple unit tests with a function in a header file.
So with something like this you can write safer code.
56 changes: 56 additions & 0 deletions unit-test/multiply-test.l1com
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// multiply-test.l1com
//
//
#include <intr.l1h>
#include <bool.l1h>
(main func)
(set int64 1 zero 0)
(set int64 1 one 1)
(set int64 1 x1 5)
(set int64 1 y1 3)
(set int64 1 ret1 15)
(set int64 1 x2 -5)
(set int64 1 y2 3)
(set int64 1 ret2 -15)
(set int64 1 ret 0)
(set bool 1 Btest_ok 1)
(set int64 1 f 0)
(set int64 1 retcode 0)
(set string s test_passstr "tests passed: all ok!")
(set string s test_failstr "tests failed!")
// run tests

// test 1
(x1 y1 :multiply !)
(ret stpop)

print_i (ret)
print_n

(((ret ret1 !=) f =) f if)
(false Btest_ok =)
(endif)

// test 2
(x2 y2 :multiply !)
(ret stpop)

print_i (ret)
print_n

(((ret ret2 !=) f =) f if)
(false Btest_ok =)
(endif)

// check
(((Btest_ok false ==) f =) f if+)
print_s (test_failstr)
(one retcode =)
(else)
print_s (test_passstr)
(zero retcode =)
(endif)
print_n
exit (retcode)
(funcend)
#include <multiply.l1h>
16 changes: 16 additions & 0 deletions unit-test/multiply.l1com
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// multiply.l1com
//
//
#include <intr.l1h>
(main func)
(set int64 1 zero 0)
(set int64 1 x 5)
(set int64 1 y 3)
(set int64 1 ret 0)
(x y :multiply !)
(ret stpop)
print_i (ret)
print_n
exit (zero)
(funcend)
#include <multiply.l1h>
10 changes: 10 additions & 0 deletions unit-test/multiply.l1h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(multiply func)
#var ~ multiply

(set int64 1 x~ 0)
(set int64 1 y~ 0)
(set int64 1 ret~ 0)
(y~ x~ stpop)
{ret~ = (x~ * y~)}
(ret~ stpush)
(funcend)

0 comments on commit 3dc666e

Please sign in to comment.