From 6798d45e5efbb16a9371094ed2094ceb8059316a Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Thu, 30 Aug 2018 13:20:46 +0300 Subject: [PATCH] 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. --- appveyor.yml | 7 +------ ranges/typedranges.nim | 11 ++++++++--- tests/ttypedranges.nim | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 206dfec..2b5cbc3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -24,13 +24,8 @@ install: - IF not exist "%MINGW_ARCHIVE%" appveyor DownloadFile "%MINGW_URL%" -FileName "%MINGW_ARCHIVE%" - 7z x -y "%MINGW_ARCHIVE%" > nul - 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 - - 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 - cd csources - IF "%PLATFORM%" == "x64" ( build64.bat ) else ( build.bat ) diff --git a/ranges/typedranges.nim b/ranges/typedranges.nim index 4cc09ee..5b5f0bb 100644 --- a/ranges/typedranges.nim +++ b/ranges/typedranges.nim @@ -12,7 +12,7 @@ type mLen: int # A view into mutable array - MutRange*[T] = distinct Range[T] + MutRange* {.shallow.} [T] = distinct Range[T] ByteRange* = Range[byte] MutByteRange* = MutRange[byte] @@ -129,7 +129,7 @@ proc sliceNormalized[T](r: Range[T], ibegin, iend: int): Range[T] = # an empty range when rangesGCHoldEnabled: - result.gcHold = r.gcHold + shallowCopy(result.gcHold, r.gcHold) result.start = r.start.shift(ibegin) 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) 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 # this preferred syntax doesn't work diff --git a/tests/ttypedranges.nim b/tests/ttypedranges.nim index 74d1223..433b484 100644 --- a/tests/ttypedranges.nim +++ b/tests/ttypedranges.nim @@ -72,8 +72,11 @@ suite "Typed ranges": var r2 = r s[0] = 5 check(r[0] == 5) - r[1] = 10 + s[1] = 10 check(r2[1] == 10) + var r3 = r[2..2] + s[2] = 15 + check(r3[0] == 15) test "hash function": var a = toRange(@[1,2,3]) @@ -103,6 +106,34 @@ suite "Typed ranges": var a = toRange(@[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)": var a: Range[int] check: