Skip to content

Commit

Permalink
Fix the generative tests broken by PR #41 (redux) (#45)
Browse files Browse the repository at this point in the history
* Use https repository for usethesource.io

This repository is necessary to get the io.usethesource.capsule  test dependency.

Since maven 3.8.1 HTTP repositories are blocked by default. See
https://maven.apache.org/docs/3.8.1/release-notes.html

* fix DiffList linear and forked methods

* Reset the slice when doing a linear set op on a DiffList.

I believe it needs to be reset because the new underlying collection is
the result of applying the old slice, so it should never be sliced
again.

This resolves the error in test-linear-list that shrinks to
[:concat [0 0]] [:diff-wrap] [:linear] [:remove-last] [:set 0 0]

* Maintain linear property in DiffList on prefix, suffix and slice while slicing.

* Clean up formatting on my change a bit

---------

Co-authored-by: Titouan Vervack <[email protected]>
Co-authored-by: Mike Barborak <[email protected]>
Co-authored-by: Zach Tellman <[email protected]>
  • Loading branch information
4 people authored Nov 21, 2023
1 parent 3037737 commit ca22569
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/io/lacuna/bifurcan/diffs/DiffList.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public IList<V> slice(long start, long end) {
long pSize = prefix().size();
List<V> prefix = start < pSize
? this.prefix.slice(start, Math.min(pSize, end))
: List.EMPTY;
: (isLinear() ? List.EMPTY.linear() : List.EMPTY);

start -= pSize;
end -= pSize;
Expand All @@ -79,11 +79,15 @@ public IList<V> slice(long start, long end) {

List<V> suffix = end > 0
? this.suffix.slice(max(0, start), end)
: List.EMPTY;
: (isLinear()
? List.EMPTY.linear()
: List.EMPTY);

return slicePrime.size(underlying) == 0
? Lists.concat(prefix, suffix)
: new DiffList<>(underlying, prefix, suffix, slicePrime);
: (isLinear()
? new DiffList<>(underlying, prefix, suffix, slicePrime)
: new DiffList<>(underlying, prefix, suffix, slicePrime).linear());
}

@Override
Expand Down Expand Up @@ -170,6 +174,7 @@ public IList<V> set(long idx, V value) {
IList<V> underlyingPrime = new ConcatList<>(slice.apply(underlying)).set(idx, value);
if (isLinear()) {
underlying = underlyingPrime;
this.slice = Slice.FULL;
return this;
} else {
return new DiffList<>(underlyingPrime, prefix, suffix, Slice.FULL);
Expand All @@ -188,12 +193,12 @@ public boolean isLinear() {

@Override
public DiffList<V> forked() {
return isLinear() ? new DiffList<>(underlying, prefix.linear(), suffix.linear(), slice) : this;
return isLinear() ? new DiffList<>(underlying, prefix.forked(), suffix.forked(), slice) : this;
}

@Override
public DiffList<V> linear() {
return isLinear() ? this : new DiffList<>(underlying, prefix.forked(), suffix.forked(), slice);
return isLinear() ? this : new DiffList<>(underlying, prefix.linear(), suffix.linear(), slice);
}

@Override
Expand Down
12 changes: 11 additions & 1 deletion test/bifurcan/list_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[clojure.test :refer :all])
(:import
[io.lacuna.bifurcan
List]))
LinearList List]))

;; Access some private constants in the Java implementation, so some
;; tests can be parameterized based upon those values.
Expand Down Expand Up @@ -92,3 +92,13 @@
(is (= true (same-seq l3 r3)))
(let [l4 (.slice l3 1 10)]
(is (= true (same-seq l4 r4))))))

(deftest github-issue-40-test
(let [l1 (.slice (LinearList/from (range 1 3)) 0 1)
l2 (.linear l1)
l3 (.forked l2)]
(is (= (same-seq l2 (range 1 2)) true) "l2 has correct contents")
(is (= (same-seq l3 (range 1 2)) true) "l3 has correct contents")
(is (= (.isLinear l2) true) "l2 is linear")
(is (= (.isLinear l3) false) "l3 is forked")))

0 comments on commit ca22569

Please sign in to comment.