Fix shallow for MutRange[T]. (#29)

* Fix shallow for MutRange[T].
Add shallow for slices of Range[T] and MutRange[T].
Add testing procedure.
Add tests.

* Fix appveyor to use status-im nim compiler.
This commit is contained in:
Eugene Kabanov 2018-08-30 13:20:46 +03:00 committed by Yuriy Glukhov
parent db888bc5ae
commit 6798d45e5e
3 changed files with 41 additions and 10 deletions

View File

@ -24,13 +24,8 @@ install:
- IF not exist "%MINGW_ARCHIVE%" appveyor DownloadFile "%MINGW_URL%" -FileName "%MINGW_ARCHIVE%" - IF not exist "%MINGW_ARCHIVE%" appveyor DownloadFile "%MINGW_URL%" -FileName "%MINGW_ARCHIVE%"
- 7z x -y "%MINGW_ARCHIVE%" > nul - 7z x -y "%MINGW_ARCHIVE%" > nul
- SET PATH=%CD%\%MINGW_DIR%\bin;%CD%\Nim\bin;%PATH% - SET PATH=%CD%\%MINGW_DIR%\bin;%CD%\Nim\bin;%PATH%
- git clone https://github.com/nim-lang/Nim.git - git clone https://github.com/status-im/Nim.git
- cd %CD%\Nim - cd %CD%\Nim
- git remote add statusim https://github.com/status-im/Nim.git
- git fetch statusim
- git config --global user.email "you@example.com"
- git config --global user.name "Your Name"
- for /f "tokens=*" %%G IN ('git branch -a --list ^"statusim/status-autopatch-*^"') DO (git merge %%G)
- git clone --depth 1 https://github.com/nim-lang/csources - git clone --depth 1 https://github.com/nim-lang/csources
- cd csources - cd csources
- IF "%PLATFORM%" == "x64" ( build64.bat ) else ( build.bat ) - IF "%PLATFORM%" == "x64" ( build64.bat ) else ( build.bat )

View File

@ -12,7 +12,7 @@ type
mLen: int mLen: int
# A view into mutable array # A view into mutable array
MutRange*[T] = distinct Range[T] MutRange* {.shallow.} [T] = distinct Range[T]
ByteRange* = Range[byte] ByteRange* = Range[byte]
MutByteRange* = MutRange[byte] MutByteRange* = MutRange[byte]
@ -129,7 +129,7 @@ proc sliceNormalized[T](r: Range[T], ibegin, iend: int): Range[T] =
# an empty range # an empty range
when rangesGCHoldEnabled: when rangesGCHoldEnabled:
result.gcHold = r.gcHold shallowCopy(result.gcHold, r.gcHold)
result.start = r.start.shift(ibegin) result.start = r.start.shift(ibegin)
result.mLen = iend - ibegin + 1 result.mLen = iend - ibegin + 1
@ -169,7 +169,12 @@ proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: Range[T]) {.inline.} =
r[s] = toOpenArray(v) r[s] = toOpenArray(v)
proc baseAddr*[T](r: Range[T]): ptr T {.inline.} = r.start proc baseAddr*[T](r: Range[T]): ptr T {.inline.} = r.start
proc gcHolder*[T](r: Range[T]): ptr T {.inline.} =
## This procedure is used only for shallow test, do not use it
## in production.
when rangesGCHoldEnabled:
if r.len > 0:
result = unsafeAddr r.gcHold[0]
template toRange*[T](a: Range[T]): Range[T] = a template toRange*[T](a: Range[T]): Range[T] = a
# this preferred syntax doesn't work # this preferred syntax doesn't work

View File

@ -72,8 +72,11 @@ suite "Typed ranges":
var r2 = r var r2 = r
s[0] = 5 s[0] = 5
check(r[0] == 5) check(r[0] == 5)
r[1] = 10 s[1] = 10
check(r2[1] == 10) check(r2[1] == 10)
var r3 = r[2..2]
s[2] = 15
check(r3[0] == 15)
test "hash function": test "hash function":
var a = toRange(@[1,2,3]) var a = toRange(@[1,2,3])
@ -103,6 +106,34 @@ suite "Typed ranges":
var a = toRange(@[1,2,3]) var a = toRange(@[1,2,3])
check $a.toOpenArray == "[1, 2, 3]" check $a.toOpenArray == "[1, 2, 3]"
test "MutRange[T] shallow test":
var b = @[1, 2, 3, 4, 5, 6]
var r1 = b.toRange()
var r2 = r1
b[0] = 5
b[1] = 10
b[2] = 15
var r3 = r1[1..1]
var a0 = cast[uint](addr b[0])
var a1 = cast[uint](r1.gcHolder)
var a2 = cast[uint](r2.gcHolder)
var a3 = cast[uint](r3.gcHolder)
check:
a1 == a0
a2 == a0
a3 == a0
test "Range[T] shallow test":
var r1 = toRange(@[1, 2, 3, 4, 5, 6])
var r2 = r1
var r3 = r1[1..1]
var a1 = cast[uint](r1.gcHolder)
var a2 = cast[uint](r2.gcHolder)
var a3 = cast[uint](r3.gcHolder)
check:
a2 == a1
a3 == a1
test "tryAdvance(Range)": test "tryAdvance(Range)":
var a: Range[int] var a: Range[int]
check: check: