From 8a1424669f6269db0b1ba94d3ab43b75270272bd Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 26 Jun 2017 21:03:39 +0200 Subject: [PATCH] optimized diff algorithm further; fixed a stupid bug; cleaned up the tests --- src/jdict.nim | 2 + src/karax.nim | 30 +++--- tests/diffDomTests.html | 1 - tests/diffDomTests.nim | 223 +++++++++++++--------------------------- tests/difftest.nim | 29 +++++- 5 files changed, 117 insertions(+), 168 deletions(-) diff --git a/src/jdict.nim b/src/jdict.nim index f541ac7..b239af2 100644 --- a/src/jdict.nim +++ b/src/jdict.nim @@ -20,3 +20,5 @@ proc `[]=`*[T](s: JSeq[T], i: int, v: T) {.importcpp: "#[#] = #", noSideEffect.} proc newJSeq*[T](): JSeq[T] {.importcpp: "[@]".} proc len*[T](s: JSeq[T]): int {.importcpp: "#.length", noSideEffect.} proc add*[T](s: JSeq[T]; x: T) {.importcpp: "#.push(#)", noSideEffect.} + +proc shrink*[T](s: JSeq[T]; shorterLen: int) {.importcpp: "#.length = #", noSideEffect.} diff --git a/src/karax.nim b/src/karax.nim index 3f3ebed..f07bf71 100644 --- a/src/karax.nim +++ b/src/karax.nim @@ -208,12 +208,13 @@ type parent, current: Node n: VNode -proc addPatch(patches: var seq[Patch]; k: PatchKind; parent, current: Node; +proc addPatch(patches: JSeq[Patch]; k: PatchKind; parent, current: Node; n: VNode) = patches.add(Patch(k: k, parent: parent, current: current, n: n)) -proc apply(patches: seq[Patch]; kxi: KaraxInstance) = - for p in patches: +proc apply(patches: JSeq[Patch]; kxi: KaraxInstance) = + for i in 0.. Diff dom tests - diff --git a/tests/diffDomTests.nim b/tests/diffDomTests.nim index d2c09ab..e28c6f6 100644 --- a/tests/diffDomTests.nim +++ b/tests/diffDomTests.nim @@ -2,182 +2,107 @@ import kdom, vdom, times, karax, karaxdsl, jdict, jstrutils, parseutils, sequtils var - entries: seq[cstring] - results: seq[cstring] - timeout: Timeout + entries: seq[cstring] + results: seq[cstring] proc reset() = - results.add cstring"reset started" - entries = @[cstring("0"), cstring("1"), cstring("2"), cstring("3"), cstring("4"), cstring("5") ] - redraw() - results.add cstring"reset finished" + entries = @[cstring("0"), cstring("1"), cstring("2"), cstring("3"), cstring("4"), cstring("5")] + redrawSync() -proc checkOrder(order : seq[int]): bool = - var ul = getElementById("ul") - if ul == nil or len(ul.children) != len(order): - return false - var pos = 0 - for child in ul.children: - if child.id != $order[pos]: - return false - inc pos - return true +proc checkOrder(order: seq[int]): bool = + var ul = getElementById("ul") + if ul == nil or len(ul.children) != len(order): + kout ul, len(order) + return false + var pos = 0 + for child in ul.children: + if child.id != $order[pos]: + kout pos + return false + inc pos + return true -proc check1() = - let result = checkOrder(@[0, 1, 2, 3, 4, 7, 5]) - if result: - results.add cstring"test1 - OK" - else: - results.add cstring"test1 - FAIL" +proc check(name: cstring; order: seq[int]) = + let result = checkOrder(order) + results.add name & (if result: cstring" - OK" else: cstring" -FAIL") # result: 0 1 2 3 4 7 5 proc test1() = - results.add cstring"test1 started" - entries = @[cstring("0"), cstring("1"), cstring("2"), cstring("3"), cstring("4"), cstring("5") ] - entries.insert(cstring("7"), 5) - redraw() - timeout = setTimeout(check1, 20) + results.add cstring"test1 started" + entries = @[cstring("0"), cstring("1"), cstring("2"), cstring("3"), cstring("4"), cstring("5")] + entries.insert(cstring("7"), 5) + redrawSync() + check("test1", @[0, 1, 2, 3, 4, 7, 5]) -proc check2() = - let result = checkOrder(@[8, 0, 1, 2, 3, 4, 7, 5]) - if result: - results.add cstring"test1 - OK" - else: - results.add cstring"test2 - FAIL" - -# result: 8 0 1 2 3 4 7 5 proc test2() = - results.add cstring"test2 started" - entries = @[cstring("0"), cstring("1"), cstring("2"), cstring("3"), cstring("4"), cstring("5") ] - entries.insert(cstring("7"), 5) - entries.insert(cstring("8"), 0) - redraw() - timeout = setTimeout(check2, 20) + results.add cstring"test2 started" + entries = @[cstring("0"), cstring("1"), cstring("2"), cstring("3"), cstring("4"), cstring("5")] + entries.insert(cstring("7"), 5) + entries.insert(cstring("8"), 0) + redrawSync() + check("test2", @[8, 0, 1, 2, 3, 4, 7, 5]) -proc check3() = - let result = checkOrder(@[2, 3, 4, 1]) - if result: - results.add cstring"test3 - OK" - else: - results.add cstring"test3 - FAIL" - -# result: 2 3 4 1 proc test3() = - results.add cstring"test3 started" - entries = @[cstring("2"), cstring("3"), cstring("4"), cstring("1") ] - redraw() - timeout = setTimeout(check3, 20) + results.add cstring"test3 started" + entries = @[cstring("2"), cstring("3"), cstring("4"), cstring("1")] + redrawSync() + check("test3", @[2, 3, 4, 1]) -proc check4() = - let result = checkOrder(@[5, 6, 7, 8]) - if result: - results.add cstring"test4 - OK" - else: - results.add cstring"test4 - FAIL" - -# result: 5 6 7 8 proc test4() = - results.add cstring"test4 started" - entries = @[cstring("5"), cstring("6"), cstring("7"), cstring("8") ] - redraw() - timeout = setTimeout(check4, 20) + results.add cstring"test4 started" + entries = @[cstring("5"), cstring("6"), cstring("7"), cstring("8") ] + redrawSync() + check("test4", @[5, 6, 7, 8]) -proc check5() = - let result = checkOrder(@[0, 1, 3, 5, 4, 5]) - if result: - results.add cstring"test5 - OK" - else: - results.add cstring"test5 - FAIL" - -# result: 0 1 3 5 4 5 proc test5() = - results.add cstring"test5 started" - entries = @[cstring("0"), cstring("1"), cstring("3"), cstring("5"), cstring("4"), cstring("5") ] - redraw() - timeout = setTimeout(check5, 20) + results.add cstring"test5 started" + entries = @[cstring("0"), cstring("1"), cstring("3"), cstring("5"), cstring("4"), cstring("5")] + redrawSync() + check("test 5", @[0, 1, 3, 5, 4, 5]) -proc check6() = - let result = checkOrder(@[]) - if result: - results.add cstring"test6 - OK" - else: - results.add cstring"test6 - FAIL" - -# result: empty proc test6() = - results.add cstring"test6 started" - entries = @[] - redraw() - timeout = setTimeout(check6, 20) - -proc check7() = - let result = checkOrder(@[2]) - if result: - results.add cstring"test7 - OK" - else: - results.add cstring"test7 - FAIL" - redraw() + results.add cstring"test6 started" + entries = @[] + redrawSync() + check("test 6", @[]) # result: 2 proc test7() = - results.add cstring"test7 started" - entries = @[cstring("2")] - redraw() - timeout = setTimeout(check7, 20) + results.add cstring"test7 started" + entries = @[cstring("2")] + redrawSync() + check("test 7", @[2]) proc createEntry(id: int): VNode = result = buildHtml(): button(id="" & $id): - text $id + text $id proc createDom(): VNode = - result = buildHtml(tdiv()): - ul(id="ul"): - for e in entries: - createEntry(parseInt(e)) - for r in results: - tdiv: - text r + result = buildHtml(tdiv()): + ul(id="ul"): + for e in entries: + createEntry(parseInt(e)) + for r in results: + tdiv: + text r proc onload() = - for i in 0..5: # 0_000: - entries.add(cstring($i)) + for i in 0..5: # 0_000: + entries.add(cstring($i)) + test1() + reset() + test2() + reset() + test3() + reset() + test4() + reset() + test5() + reset() + test6() + reset() + test7() - var dtReset = 100 - var dtTest = 500 - - var t = dtReset - timeout = setTimeout(test1, t) - t += dtTest - timeout = setTimeout(reset, t) - t += dtReset - - timeout = setTimeout(test2, t) - t += dtTest - timeout = setTimeout(reset, t) - t += dtReset - - timeout = setTimeout(test3, t) - t += dtTest - timeout = setTimeout(reset, t) - t += dtReset - - timeout = setTimeout(test4, t) - t += dtTest - timeout = setTimeout(reset, t) - t += dtReset - - timeout = setTimeout(test5, t) - t += dtTest - timeout = setTimeout(reset, t) - t += dtReset - - timeout = setTimeout(test6, t) - t += dtTest - timeout = setTimeout(reset, t) - t += dtReset - - timeout = setTimeout(test7, t) - -onload() setRenderer createDom +onload() diff --git a/tests/difftest.nim b/tests/difftest.nim index 9f80037..bd90b6a 100644 --- a/tests/difftest.nim +++ b/tests/difftest.nim @@ -2,10 +2,19 @@ include "../src/karax" import "../src/karaxdsl" +proc hasDom(n: Vnode) = + if n.kind in {VNodeKind.component, VNodeKind.vthunk, VNodeKind.dthunk}: + discard + else: + doAssert n.dom != nil + for i in 0..