Dragging in some modules when X86Compiler is used, so scripts can use them.
[trylon.git] / tests / posix / main
blob14455f0af39f8a8dfb642e29b72673f0d44b8d2d
1 trylon posix-test
3 main: args
4         try
5                 fd = Posix open: "main" flags: 'O_RDONLY'
6                 print-line: "fd = " + fd string
7                 if fd == -1
8                         errno = Posix errno
9                         err-string = Posix strerror: errno
10                         print-line: "errno = " + errno string + " (" + err-string + ")"
11                         return 1
12                 
13                 fds = Posix FDSet new
14                 fds set: fd
15                 result =        --
16                         Posix select: fd + 1 reads: fds writes: nil exceptions: nil timeout: nil
17                 print-line: "select result = " + result string
18                 if fds at: fd
19                         print-line: "The fd can read without blocking."
21                 Posix close-fd: fd
22                 print-line
24                 # Processes: fork, pipe.
25                 Test name: "fork/wait" check: test-fork
26                 Test name: "pipe" check: test-pipe
27                 Test name: "exec" check: test-exec
29                 # Regex.
30                 Test name: "Regex" check: regex-test
32                 # Directories.
33                 Test name: "directories" check: directory-test
34         
35         else
36                 send: exception message
39 test-fork
40         exit-status = 33
41         child = Posix fork
42         if child == 0
43                 # We *are* the child.
44                 System sleep-ms: 250
45                 send: "From the child."
47                 # Exit directly, skipping the test result.
48                 Posix exit: exit-status
49         else
50                 # We're the parent.
51                 send: "From the parent (child = ", child, ")."
52                 wait-result = Posix wait-pid: child
53                 # OR: wait-result = Posix wait
54                 if !wait-result is-valid || !wait-result exited
55                         return false
56                 if wait-result exit-status != exit-status
57                         return false
59         return true
62 test-pipe
63         values = "foo", "bar", "baz"
64         pipe-fds = Posix pipe
65         child = Posix fork
66         if child == 0
67                 # This is the child.
68                 Posix close-fd: pipe-fds read
69                 out-stream = Posix FDOutputStream new: pipe-fds write
70                 for value in values
71                         out-stream send: value
72                 out-stream close
73                 Posix exit: 0
74         else
75                 # This is the parent.
76                 Posix close-fd: pipe-fds write
77                 in-stream = Posix FDInputStream new: pipe-fds read
78                 lines = StreamLinesReader new: in-stream
79                 ok = true
80                 for value in values
81                         received-value = lines next
82                         if received-value != value
83                                 ok = false
84                                 break
85                 if lines next != nil
86                         ok = false
87                 in-stream close
88                 return ok
91 test-exec
92         pipe-fds = Posix pipe
93         child = Posix fork
94         if child == 0
95                 # This is the child.
96                 try
97                         Posix close-fd: pipe-fds read
98                         Posix dup-from: pipe-fds write to: Posix stdout-fd
99                         Posix execp: "echo" arguments: "echo", "foo"
100                 Posix exit: 1
101         else
102                 # This is the parent.
103                 Posix close-fd: pipe-fds write
104                 in-stream = Posix FDInputStream new: pipe-fds read
105                 lines = StreamLinesReader new: in-stream
106                 line = lines next
107                 ok = (line && line == "foo")
108                 in-stream close
109                 return ok
112 regex-test
113         # Simple matching.
114         regex = Posix Regex new: "foo"
115         if !regex matches: "boo foo hoo"
116                 return false
117         if regex matches: "bar baz"
118                 return false
119         
120         # Getting results.
121         regex = Posix Regex new: "(f[a-z]*) (f[a-z]*) (f[[:alnum:]]*)" flags: 'extended'
122         haystack = "foo flarf finagle bar baz xoof"
123         expected-results = List from: "foo flarf finagle", "foo", "flarf", "finagle"
124         for match in regex matches: haystack
125                 if (match in: haystack) != expected-results pop-front
126                         return false
127         if !expected-results is-empty
128                 return false
130         return true
133 directory-test
134         ok = true
135         dir-name = "test-mkdir"
136         try
137                 start-wd = Posix getcwd
138                 Posix mkdir: dir-name mode: 511
139                 try
140                         Posix chdir: dir-name
141                         Posix chdir: start-wd
142                         Posix rmdir: dir-name
143                 else
144                         ok = false
145                         Posix chdir: start-wd
146                         Posix rmdir: dir-name
147                         throw exception
148         else
149                 ok = false
150         return ok