Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterator.toCharSeq() error #1685

Closed
ashrwin opened this issue Nov 17, 2016 · 6 comments
Closed

Iterator.toCharSeq() error #1685

ashrwin opened this issue Nov 17, 2016 · 6 comments

Comments

@ashrwin
Copy link
Member

ashrwin commented Nov 17, 2016

The following code fails

@Test
public void fail(){
    CharSeq seq = Iterator.of('a','b','c').toCharSeq();
    assertThat(seq.toString()).isEqualTo("abc");
}

with the following error

org.junit.ComparisonFailure: 
Expected :"abc"
Actual   :"Iterator(?)"

The problem is in this method

//Value.java
default CharSeq toCharSeq() {
    return CharSeq.of(toString());
}

Here toString() calls AbstractIterator.toString()

@ashrwin
Copy link
Member Author

ashrwin commented Nov 17, 2016

Changing the toCharSeq() method to this works

@SuppressWarnings("unchecked")
default CharSeq toCharSeq() {
    return CharSeq.ofAll((Iterable<? extends Character>) iterator());
}

@danieldietrich Is there any better way of fixing this?

@danieldietrich
Copy link
Contributor

danieldietrich commented Nov 17, 2016

Good catch!

Please do it this way:

interface Value<T> {

    default CharSeq toCharSeq() {
        if (this instanceof CharSeq) {
            return (CharSeq) this;
        } if (isEmpty()) {
            return CharSeq.empty();
        } else {
            return CharSeq.of(iterator().mkString());
        }
    }

}

Update: @ashrko619 I realized that the if (this instanceof Traversable) branch does not help very much - the overall complexity remains O(n). Therefore I removed it in the code above. Please consider it in your PR.

@danieldietrich
Copy link
Contributor

@ashrko619 please do this in a separate PR - after it is fixed we can pull #1684

@ashrwin
Copy link
Member Author

ashrwin commented Nov 17, 2016

@danieldietrich There a lots of errors in the tests after fixing the toCharSeq method. For ex.

@Test
public void shouldConvertToCharSeq() {
    final Value<Integer> value = of(1, 2, 3);
    final CharSeq charSeq = value.toCharSeq();
    assertThat(charSeq).isEqualTo(CharSeq.of(value.toString()));
}
org.junit.ComparisonFailure: 
Expected :Vector(1, 2, 3)
Actual   :123

Should we create a new issue for fixing these tests?

@danieldietrich
Copy link
Contributor

Mmhh, this will definitely change the semantics (and therefore won't be behavioral backward compatible any more) - but it is worth it. The current behavior can be seen as a bug.

Let's fix the tests in this issue. I think it should not be much work because several errors will depend on tests in an abstract test class. Fixing these tests will multiply regarding the specific collection tests.

If you think it is too much I'm able to do it this evening (approx. in 5 hours).

@ashrwin
Copy link
Member Author

ashrwin commented Nov 17, 2016

@danieldietrich No problems, I have already started working on fixing them 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants