Skip to content

Commit

Permalink
Fixes to PositionAfter expected sequence generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl committed Aug 1, 2024
1 parent b5b16c6 commit cf29730
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
56 changes: 39 additions & 17 deletions assets/pango/movement/movement.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ const (
ActionWhereAfter ActionWhereType = "after"
)

type entryPositionType int

const (
entryPositionBefore entryPositionType = iota
entryPositionAfter
)

type Movable interface {
EntryName() string
}
Expand Down Expand Up @@ -179,41 +172,69 @@ func processPivotMovement(entries []Movable, existing []Movable, pivot Movable,

filteredPivotIdx := findPivotIdx(filtered, pivot)

slog.Debug("pivot()", "existing", existing, "filtered", filtered, "filteredPivotIdx", filteredPivotIdx)
switch movement {
case movementBefore:
expectedIdx := 0
for ; expectedIdx < filteredPivotIdx; expectedIdx++ {
expected[expectedIdx] = filtered[expectedIdx]
}

slog.Debug("pivot()", "expected", expected)

for _, elt := range entries {
expected[expectedIdx] = elt
expectedIdx++
}

slog.Debug("pivot()", "expected", expected)

expected[expectedIdx] = pivot
expectedIdx++

slog.Debug("pivot()", "expected", expected)

filteredLen := len(filtered)
for i := filteredPivotIdx + 1; i < filteredLen; i++ {
expected[expectedIdx] = filtered[i]
expectedIdx++
}

slog.Debug("pivot()", "expected", expected)
case movementAfter:
slog.Debug("pivot()", "filtered", filtered)
expectedIdx := 0
for ; expectedIdx < len(filtered); expectedIdx++ {
for ; expectedIdx < filteredPivotIdx+1; expectedIdx++ {
expected[expectedIdx] = filtered[expectedIdx]
}

for _, elt := range entries {
expected[expectedIdx] = elt
expectedIdx++
}
if direct {
for _, elt := range entries {
expected[expectedIdx] = elt
expectedIdx++
}

filteredLen := len(filtered)
for i := filteredPivotIdx + 1; i < filteredLen-1; i++ {
expected[expectedIdx] = filtered[i]
expectedIdx++
slog.Debug("pivot()", "expected", expected)

filteredLen := len(filtered)
for i := filteredPivotIdx + 1; i < filteredLen; i++ {
expected[expectedIdx] = filtered[i]
}
} else {
filteredLen := len(filtered)
for i := filteredPivotIdx + 1; i < filteredLen; i++ {
expected[expectedIdx] = filtered[i]
expectedIdx++
}

slog.Debug("pivot()", "expected", expected)

for _, elt := range entries {
expected[expectedIdx] = elt
expectedIdx++
}

slog.Debug("pivot()", "expected", expected)
}
}

Expand Down Expand Up @@ -264,7 +285,7 @@ func updateSimulatedIdxMap(idxMap *map[Movable]int, moved Movable, startingIdx i
}
}

func OptimizeMovements(existing []Movable, expected []Movable, entries []Movable, actions []MoveAction, position any) []MoveAction {
func OptimizeMovements(existing []Movable, expected []Movable, entries []Movable, actions []MoveAction, position Position) []MoveAction {
simulated := make([]Movable, len(existing))
copy(simulated, existing)

Expand Down Expand Up @@ -309,6 +330,7 @@ func OptimizeMovements(existing []Movable, expected []Movable, entries []Movable
}

func GenerateMovements(existing []Movable, expected []Movable, entries []Movable, movement movementType) ([]MoveAction, error) {
slog.Debug("GenerateMovements()", "existing", existing, "expected", expected)
if len(existing) != len(expected) {
return nil, ErrSlicesNotEqualLength
}
Expand Down
25 changes: 24 additions & 1 deletion assets/pango/movement/movement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var _ = Describe("Movement", func() {
Expect(moves).To(HaveLen(0))
})
Context("and moved entries are out of order", func() {
It("should generate a single command to move B before D", func() {
FIt("should generate a single command to move B before D", func() {
// A B C D E -> A B C E D
entries := asMovable([]string{"E", "D"})
moves, err := movement.MoveGroup(
Expand All @@ -149,6 +149,27 @@ var _ = Describe("Movement", func() {
})
})
})
Context("when direct position relative to the pivot is required", func() {
It("should generate required move actions", func() {
// A B C D E -> C D A B E
entries := asMovable([]string{"A", "B"})
moves, err := movement.MoveGroup(
movement.PositionAfter{Directly: true, Pivot: Mock{"D"}},
entries, existing,
)

Expect(err).ToNot(HaveOccurred())
Expect(moves).To(HaveLen(2))

Expect(moves[0].Movable.EntryName()).To(Equal("A"))
Expect(moves[0].Where).To(Equal(movement.ActionWhereAfter))
Expect(moves[0].Destination.EntryName()).To(Equal("D"))

Expect(moves[1].Movable.EntryName()).To(Equal("B"))
Expect(moves[1].Where).To(Equal(movement.ActionWhereAfter))
Expect(moves[1].Destination.EntryName()).To(Equal("A"))
})
})

})
Context("With PositionBefore used as position", func() {
Expand Down Expand Up @@ -204,6 +225,8 @@ var _ = Describe("Movement", func() {
Expect(moves[1].Movable.EntryName()).To(Equal("B"))
Expect(moves[1].Where).To(Equal(movement.ActionWhereAfter))
Expect(moves[1].Destination.EntryName()).To(Equal("A"))

Expect(true).To(BeFalse())
})
})
})
Expand Down

0 comments on commit cf29730

Please sign in to comment.