about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-08-20 18:29:09 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-08-20 18:29:09 -0700
commitebea4c3f4a3aed3bb0f6c16edd1d44eea965ca6b (patch)
tree0be5ca83da65d689db6acd0bc36509d046cf0d4f
parentf09d9076f16dbc8104a8320f54251347b1387d9f (diff)
downloadmu-ebea4c3f4a3aed3bb0f6c16edd1d44eea965ca6b.tar.gz
3231 - reading from fake files in scenarios
-rw-r--r--075channel.mu2
-rw-r--r--088file.mu47
-rw-r--r--090scenario_filesystem_test.mu23
3 files changed, 68 insertions, 4 deletions
diff --git a/075channel.mu b/075channel.mu
index 1ee716a2..72d039a9 100644
--- a/075channel.mu
+++ b/075channel.mu
@@ -67,6 +67,7 @@ def new-channel capacity:number -> in:address:source:_elem, out:address:sink:_el
 def write out:address:sink:_elem, val:_elem -> out:address:sink:_elem [
   local-scope
   load-ingredients
+  assert out, [write to null channel]
   chan:address:channel:_elem <- get *out, chan:offset
   <channel-write-initial>
   {
@@ -97,6 +98,7 @@ def write out:address:sink:_elem, val:_elem -> out:address:sink:_elem [
 def read in:address:source:_elem -> result:_elem, eof?:boolean, in:address:source:_elem [
   local-scope
   load-ingredients
+  assert in, [read on null channel]
   eof? <- copy 0/false  # default result
   chan:address:channel:_elem <- get *in, chan:offset
   {
diff --git a/088file.mu b/088file.mu
index 4e3a6dec..2eeeec2c 100644
--- a/088file.mu
+++ b/088file.mu
@@ -13,12 +13,35 @@ container file-mapping [
 def start-reading fs:address:filesystem, filename:address:array:character -> contents:address:source:character [
   local-scope
   load-ingredients
-  file:number <- $open-file-for-reading filename
-  contents:address:source:character, sink:address:sink:character <- new-channel 30
-  start-running transmit-from-file file, sink
+  {
+    break-if fs
+    # real file-system
+    file:number <- $open-file-for-reading filename
+    assert file, [file not found]
+    contents:address:source:character, sink:address:sink:character <- new-channel 30
+    start-running transmit-from-file file, sink
+    return
+  }
+  # fake file system
+  i:number <- copy 0
+  data:address:array:file-mapping <- get *fs, data:offset
+  len:number <- length *data
+  {
+    done?:boolean <- greater-or-equal i, len
+    break-if done?
+    tmp:file-mapping <- index *data, i
+    curr-filename:address:array:character <- get tmp, name:offset
+    found?:boolean <- equal filename, curr-filename
+    loop-unless found?
+    contents:address:source:character, sink:address:sink:character <- new-channel 30
+    curr-contents:address:array:character <- get tmp, contents:offset
+    start-running transmit-from-text curr-contents, sink
+    return
+  }
+  return 0/not-found
 ]
 
-def transmit-from-file file:number, sink:address:sink:character -> file:number, sink:address:sink:character [
+def transmit-from-file file:number, sink:address:sink:character -> sink:address:sink:character [
   local-scope
   load-ingredients
   {
@@ -31,6 +54,22 @@ def transmit-from-file file:number, sink:address:sink:character -> file:number,
   $close-file file
 ]
 
+def transmit-from-text contents:address:array:character, sink:address:sink:character -> sink:address:sink:character [
+  local-scope
+  load-ingredients
+  i:number <- copy 0
+  len:number <- length *contents
+  {
+    done?:boolean <- greater-or-equal i, len
+    break-if done?
+    c:character <- index *contents, i
+    sink <- write sink, c
+    i <- add i, 1
+    loop
+  }
+  sink <- close sink
+]
+
 def start-writing fs:address:filesystem, filename:address:array:character -> sink:address:sink:character, routine-id:number [
   local-scope
   load-ingredients
diff --git a/090scenario_filesystem_test.mu b/090scenario_filesystem_test.mu
new file mode 100644
index 00000000..4d4624eb
--- /dev/null
+++ b/090scenario_filesystem_test.mu
@@ -0,0 +1,23 @@
+# Check our support for fake file systems in scenarios.
+
+scenario read-from-fake-file [
+  local-scope
+  assume-filesystem [
+    [a] <- [
+      |xyz|
+    ]
+  ]
+  contents:address:source:character <- start-reading filesystem:address:filesystem, [a]
+  1:character/raw <- read contents
+  2:character/raw <- read contents
+  3:character/raw <- read contents
+  4:character/raw <- read contents
+  _, 5:boolean/raw <- read contents
+  memory-should-contain [
+    1 <- 120  # x
+    2 <- 121  # y
+    3 <- 122  # z
+    4 <- 10  # newline
+    5 <- 1  # eof
+  ]
+]