aboutsummaryrefslogtreecommitdiffhomepage
path: root/libslang/src/test/posixio.sl
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2011-10-14 04:55:05 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2011-10-14 04:55:05 +0200
commit6aa0e0017d7d0cddc006da885946934b06949a91 (patch)
tree66b688ec32e2f91266db760b1762f2a50cc52036 /libslang/src/test/posixio.sl
parenta966db5b71328f6adf9dd767e64b322a3bd7ed9c (diff)
downloaderlang-slang-fork-6aa0e0017d7d0cddc006da885946934b06949a91.tar.gz
include libslang-1.4.9 and automatically build it and link erlang-slang against it
few (erlang) people will still have libslang-1.4.9 installed or spend time to get it to link against the driver
Diffstat (limited to 'libslang/src/test/posixio.sl')
-rw-r--r--libslang/src/test/posixio.sl93
1 files changed, 93 insertions, 0 deletions
diff --git a/libslang/src/test/posixio.sl b/libslang/src/test/posixio.sl
new file mode 100644
index 0000000..d3a0dc0
--- /dev/null
+++ b/libslang/src/test/posixio.sl
@@ -0,0 +1,93 @@
+_debug_info = 1; () = evalfile ("inc.sl");
+
+
+print ("Testing POSIX I/O routines...");
+
+static define open_tmp_file (fileptr, flags, mode)
+{
+ variable n;
+ variable file, fd;
+ variable fmt;
+
+ @fileptr = NULL;
+
+ fmt = "tmp-xxx.%03d"; % I need something that works on an 8+3 filesystem
+
+ n = -1;
+ while (n < 999)
+ {
+ n++;
+ file = sprintf (fmt, n);
+ if (NULL != stat_file (file))
+ continue;
+
+ fd = open (file, flags, 0777);
+ if (fd != NULL)
+ {
+ @fileptr = file;
+ return fd;
+ }
+ }
+ failed ("Unable to open a tmp file");
+}
+
+define run_tests (some_text)
+{
+ variable file, fd, fp;
+ variable new_text, nbytes, len;
+ variable pos;
+
+ fd = open_tmp_file (&file, O_WRONLY|O_BINARY|O_CREAT, 0777);
+
+ if (-1 == write (fd, some_text))
+ failed ("write");
+
+ fp = fdopen (fd, "wb");
+ if (fp == NULL)
+ failed ("fdopen");
+
+ if (isatty (fileno (fp)))
+ failed ("isatty (fileno)");
+
+ if (-1 == close (fd))
+ failed ("close");
+
+ fd = open (file, O_RDONLY|O_BINARY);
+ if (fd == NULL) failed ("fopen existing");
+
+ len = bstrlen (some_text);
+ nbytes = read (fd, &new_text, len);
+ if (nbytes == -1)
+ failed ("read");
+
+ if ((nbytes != len)
+ or (some_text != new_text))
+ failed ("read");
+
+ if (0 != read (fd, &new_text, 1))
+ failed ("read at EOF");
+ if (bstrlen (new_text))
+ failed ("read at EOF");
+
+ if (-1 == close (fd)) failed ("close after tests");
+ variable st = stat_file (file);
+ () = st.st_mode; % see if stat_file returned the right struct
+ () = remove (file);
+ if (stat_file (file) != NULL) failed ("remove");
+}
+
+
+run_tests ("ABCDEFG");
+run_tests ("A\000BC\000\n\n\n");
+
+variable fd = open ("/dev/tty", O_RDONLY);
+if (fd != NULL)
+{
+ if (0 == isatty (fd))
+ failed ("isatty");
+}
+fd = 0;
+
+
+print ("Ok\n");
+exit (0);