summary refs log tree commit diff stats
path: root/tests/stdlib/tmemfiles2.nim
blob: 1b249898eece860de9f4f55dee6acf4f6f860122 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pre { line-height: 125%; }
td.linenos .normal { color
discard """
  disabled: "Windows"
  output: '''Full read size: 20
Half read size: 10 Data: Hello'''
"""
import memfiles, os
const
  fn = "test.mmap"
var
  mm, mm_full, mm_half: MemFile
  p: pointer

if fileExists(fn): removeFile(fn)

# Create a new file, data all zeros
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20)
mm.close()

# read, change
mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1, allowRemap = true)
let size = mm_full.size
p = mm_full.mapMem(fmReadWrite, 20, 0)
echo "Full read size: ", size
var p2 = cast[cstring](p)
p2[0] = 'H'
p2[1] = 'e'
p2[2] = 'l'
p2[3] = 'l'
p2[4] = 'o'
p2[5] = '\0'
mm_full.unmapMem(p, 20)
mm_full.close()

# read half, and verify data change
mm_half = memfiles.open(fn, mode = fmRead, mappedSize = 10)
echo "Half read size: ", mm_half.size, " Data: ", cast[cstring](mm_half.mem)
mm_half.close()

if fileExists(fn): removeFile(fn)
="w"> text) => { // Handle ^ anchor at the beginning if (pattern[0] === '^') { return matchHere(pattern.slice(1), text); } // Otherwise, check the entire string for (let i = 0; i <= text.length; i++) { if (matchHere(pattern, text.slice(i))) return true; } return false; }; const assertEqual = (actual, expected, testName) => console.log(actual === expected ? `Passed: ${testName}` : `Failed: ${testName}. Expected ${expected} but got ${actual}`); assertEqual(match("ab*c", "ac"), true, "Star match 'ab*c' vs 'ac' (zero occurrences)"); assertEqual(match("ab*c", "abbbc"), true, "Star match 'ab*c' vs 'abbbc' (multiple occurrences)"); assertEqual(match("^ab.*c$", "abc"), true, "Complex match '^ab.*c$' vs 'abc'"); assertEqual(match("^ab.*c$", "abcd"), false, "Complex mismatch '^ab.*c$' vs 'abcd'");