aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/module/test_stdin.c48
-rw-r--r--tests/termios/test_tcgetattr.c61
-rw-r--r--tests/test_core.py4
-rw-r--r--tests/test_other.py17
4 files changed, 116 insertions, 14 deletions
diff --git a/tests/module/test_stdin.c b/tests/module/test_stdin.c
new file mode 100644
index 00000000..ab84fa77
--- /dev/null
+++ b/tests/module/test_stdin.c
@@ -0,0 +1,48 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#if EMSCRIPTEN
+#include <emscripten.h>
+#endif
+
+int line = 0;
+
+void main_loop(void *arg)
+{
+ char str[10] = {0};
+ int ret;
+
+ if (line == 0) {
+ ret = fgetc(stdin);
+ if (ret != EOF) putc(ret, stdout);
+ if (ret == '\n') line++;
+ } else if (line > 0) {
+ ret = scanf("%10s", str);
+ if (ret > 0) puts(str);
+ }
+
+ if (ferror(stdin) && errno != EAGAIN) {
+ puts("error");
+ exit(EXIT_FAILURE);
+ }
+
+ if (feof(stdin)) {
+ puts("eof");
+ exit(EXIT_SUCCESS);
+ }
+
+ clearerr(stdin);
+}
+
+int main(int argc, char const *argv[])
+{
+ fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
+#if EMSCRIPTEN
+ emscripten_set_main_loop(main_loop, 60, 0);
+#else
+ while (1) main_loop(NULL); sleep(1);
+#endif
+ return 0;
+} \ No newline at end of file
diff --git a/tests/termios/test_tcgetattr.c b/tests/termios/test_tcgetattr.c
new file mode 100644
index 00000000..2b3780ee
--- /dev/null
+++ b/tests/termios/test_tcgetattr.c
@@ -0,0 +1,61 @@
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+static void create_file(const char *path, const char *buffer, int mode) {
+ int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
+ assert(fd >= 0);
+
+ int err = write(fd, buffer, sizeof(char) * strlen(buffer));
+ assert(err == (sizeof(char) * strlen(buffer)));
+
+ close(fd);
+}
+
+void setup() {
+ create_file("test.txt", "abcdefg", 0666);
+}
+
+void cleanup() {
+ unlink("test.txt");
+}
+
+void test() {
+ struct termios tc;
+ int ret;
+ int fd;
+
+ fd = open("test.txt", O_RDONLY);
+
+ ret = tcgetattr(fd, &tc);
+ assert(ret == -1);
+ assert(errno = ENOTTY);
+
+ ret = tcgetattr(STDIN_FILENO, &tc);
+ assert(!ret);
+
+ ret = tcsetattr(fd, 0, &tc);
+ assert(ret == -1);
+ assert(errno = ENOTTY);
+
+ ret = tcsetattr(STDIN_FILENO, 0, &tc);
+ assert(!ret);
+
+ close(fd);
+
+ puts("success");
+}
+
+int main() {
+ atexit(cleanup);
+ signal(SIGABRT, cleanup);
+ setup();
+ test();
+ return EXIT_SUCCESS;
+}
diff --git a/tests/test_core.py b/tests/test_core.py
index 17707c44..51face6a 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -4670,6 +4670,10 @@ The current type of b is: 9
expected = open(path_from_root('tests', 'pthread', 'specific.c.txt'), 'r').read()
self.do_run(src, expected, force_c=True)
+ def test_tcgetattr(self):
+ src = open(path_from_root('tests', 'termios', 'test_tcgetattr.c'), 'r').read()
+ self.do_run(src, 'success', force_c=True)
+
def test_time(self):
# XXX Not sure what the right output is here. Looks like the test started failing with daylight savings changes. Modified it to pass again.
src = open(path_from_root('tests', 'time', 'src.c'), 'r').read()
diff --git a/tests/test_other.py b/tests/test_other.py
index a6813b07..f583cf07 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -929,20 +929,9 @@ f.close()
self.assertContained('libf1\nlibf2\n', run_js(os.path.join(self.get_dir(), 'a.out.js')))
def test_stdin(self):
- open('main.cpp', 'w').write(r'''
-#include <stdio.h>
-int main(int argc, char const *argv[])
-{
- char str[10] = {0};
- scanf("%10s", str);
- printf("%s\n", str);
- return 0;
-}
-''')
- Building.emcc('main.cpp', output_filename='a.out.js')
- open('in.txt', 'w').write('abc')
- # node's stdin support is broken
- self.assertContained('abc', Popen(listify(SPIDERMONKEY_ENGINE) + ['a.out.js'], stdin=open('in.txt'), stdout=PIPE, stderr=PIPE).communicate()[0])
+ Building.emcc(path_from_root('tests', 'module', 'test_stdin.c'), output_filename='a.out.js')
+ open('in.txt', 'w').write('abcdef\nghijkl')
+ self.assertContained('abcdef\nghijkl\neof', run_js(os.path.join(self.get_dir(), 'a.out.js'), stdin=open('in.txt')))
def test_ungetc_fscanf(self):
open('main.cpp', 'w').write(r'''