Coder Social home page Coder Social logo

bug in WhereIndexed about go-linq HOT 7 CLOSED

ahmetb avatar ahmetb commented on May 27, 2024
bug in WhereIndexed

from go-linq.

Comments (7)

ahmetb avatar ahmetb commented on May 27, 2024

Hmm this looks legitimate. So you're saying this is a variable reused unintentionally?

Do you mind adding a test case for it so we can clearly see this fail. I'm also surprised we don't have such a case unit tested already.

from go-linq.

quexer avatar quexer commented on May 27, 2024

hi, here's the test case:

I think index behavior should be the same no matter what predicate is provided. otherwise, we could not do index-based filtering with WhereIndexed

thanks

package main

import (
	"testing"

	"github.com/ahmetb/go-linq/v3"
)

var l = []string{"a", "b", "c", "d"}

// this will fail
func TestWhereIndexedWithTruePredicate(t *testing.T) {
	var idx int
	linq.From(l).WhereIndexed(func(i int, _ interface{}) bool {
		if i != idx {
			t.Errorf("i should be increased like idx in WhereIndexed with true predicate. expected(%v), got(%v)", idx, i)
		}
		idx++
		return true
	}).Results()
}

// this will pass
func TestWhereIndexedWithFalsePredicate(t *testing.T) {
	var idx int
	linq.From(l).WhereIndexed(func(i int, _ interface{}) bool {
		if i != idx {
			t.Errorf("i should be increased like idx in WhereIndexed with false predicate. expected(%v), got(%v)", idx, i)
		}
		idx++
		return false
	}).Results()
}

// suppose there's no different between ForEachIndexed and WhereIndexed as for index behavior
// this will pass
func TestForEachIndexed(t *testing.T) {
	var idx int
	linq.From(l).ForEachIndexed(func(i int, _ interface{}) {
		if i != idx {
			t.Errorf("i should be increased like idx in ForEachIndexed. expected(%v), got(%v)", idx, i)
		}
		idx++
	})
}

from go-linq.

ahmetb avatar ahmetb commented on May 27, 2024

hi, here's the test case:

sorry I meant to say, can you send a PR with this test case?

from go-linq.

ahmetb avatar ahmetb commented on May 27, 2024

func TestWhereIndexed(t *testing.T) {

for example we have this test case, you can add it next to that.
also do you have any idea why this test case passes? sorry I'm rusty a bit on this codebase.

from go-linq.

quexer avatar quexer commented on May 27, 2024

hi @ahmetb , here's the PR:

#87

what I'm confused is all about the picture below

image

from go-linq.

ahmetb avatar ahmetb commented on May 27, 2024

Yeah I'm surprised the [9]int{1, 1, 1, 2, 1, 2, 3, 4, 2} test is passing but yours in #87 isn't.

The part you highlighted seems accurate. First param passed to predicate is the index of element (starting from 0) in the collection, second param is its value.

from go-linq.

quexer avatar quexer commented on May 27, 2024

If the highlighted part is accurate, then I'm sure there's bug in WhereIndexed

from go-linq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.