NAME=io.cache: minimal usage
FILE=malloc://1024
CMDS=<<EOF
e io.cache=true
wx 010203
p8 3
wcr
# Test write works after disabling the cache.
e io.cache=false
wx 040506
p8 3
EOF
EXPECT=<<EOF
010203
040506
EOF
RUN

NAME=io.cache: Writing after end of file
FILE=bins/other/0x3
CMDS=<<EOF
e io.cache=true
w hello
x 5
EOF
EXPECT=<<EOF
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  6865 6c6c 6f                             hello
EOF
RUN

NAME=io.cache: Writing after end of file
FILE=malloc://1024
CMDS=<<EOF
?e
b 0xf
p8
w no-cache
p8

e io.cache=true
w cache-mode
p8

# Reseting cache restores original data.
wcr
p8
w overwrite
p8
wci

# Commit cache overwrites original data.
p8

# Reset cache with no pending cache entries.
wcr
wci
p8
EOF
EXPECT=<<EOF

000000000000000000000000000000
6e6f2d636163686500000000000000
63616368652d6d6f64650000000000
6e6f2d636163686500000000000000
6f7665727772697465000000000000
6f7665727772697465000000000000
6f7665727772697465000000000000
EOF
RUN

NAME=io.cache: empty file
FILE=.empty.tmp
BROKEN=1
CMDS=<<EOF
?e
b 15
p8
w no-cache
p8
e io.cache=true
w cache-mode
p8
# Reseting cache restores original data.
wcr
p8
w overwrite
p8
wci
# Commit cache overwrites original data.
p8
# Reset cache with no pending cache entries.
wcr
wci
p8
# File was correctly writen.
cat .empty.tmp
?e
EOF
EXPECT=<<EOF

ffffffffffffffffffffffffffffff
6e6f2d6361636865ffffffffffffff
63616368652d6d6f6465ffffffffff
6e6f2d6361636865ffffffffffffff
6f7665727772697465ffffffffffff
6f7665727772697465ffffffffffff
6f7665727772697465ffffffffffff
overwrite
EOF
RUN

NAME=io.cache: Reading behind cache written area
FILE=malloc://32
CMDS=<<EOF
e io.cache=true
px 32
sd +1
px 31
sd -1
wx 90
px 32
sd +1
px 31
EOF
EXPECT=<<EOF
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  0000 0000 0000 0000 0000 0000 0000 0000  ................
0x00000010  0000 0000 0000 0000 0000 0000 0000 0000  ................
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000001  0000 0000 0000 0000 0000 0000 0000 0000  ................
0x00000011  0000 0000 0000 0000 0000 0000 0000 00    ...............
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  9000 0000 0000 0000 0000 0000 0000 0000  ................
0x00000010  0000 0000 0000 0000 0000 0000 0000 0000  ................
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000001  0000 0000 0000 0000 0000 0000 0000 0000  ................
0x00000011  0000 0000 0000 0000 0000 0000 0000 00    ...............
EOF
RUN

NAME=io.cache: cache write, cache undo
FILE=malloc://1024
CMDS=<<EOF
e io.cache=true
w a
w b @ 1
p8 2
wc- 0 2
p8 2
EOF
EXPECT=<<EOF
6162
0000
EOF
RUN

NAME=io.cache: cache reset
FILE=malloc://32
CMDS=<<EOF
e io.cache=true
wx 90
wcr
wx 90
EOF
EXPECT_ERR=<<EOF
EOF
RUN
