/*
 * Copyright 2015 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gs.collections.test.map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.bag.UnsortedBag;
import com.gs.collections.api.map.UnsortedMapIterable;
import com.gs.collections.impl.factory.Bags;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.test.UnorderedIterableTestCase;
import com.gs.collections.test.bag.TransformsToBagTrait;
import org.junit.Assert;
import org.junit.Test;

import static com.gs.collections.test.IterableTestCase.assertEquals;
import static org.hamcrest.Matchers.isOneOf;
import static org.junit.Assert.assertThat;

public interface UnsortedMapIterableTestCase extends MapIterableTestCase, UnorderedIterableTestCase, TransformsToBagTrait
{
    @Override
    <T> UnsortedMapIterable<Object, T> newWith(T... elements);

    @Override
    default <T> UnsortedBag<T> getExpectedFiltered(T... elements)
    {
        return Bags.immutable.with(elements);
    }

    @Override
    default <T> MutableBag<T> newMutableForFilter(T... elements)
    {
        return Bags.mutable.with(elements);
    }

    @Override
    @Test
    default void RichIterable_makeString_appendString()
    {
        RichIterable<Integer> iterable = this.newWith(2, 2, 1);
        assertThat(iterable.makeString(), isOneOf("2, 2, 1", "1, 2, 2", "2, 1, 2"));
        assertThat(iterable.makeString("/"), isOneOf("2/2/1", "1/2/2", "2/1/2"));
        assertThat(iterable.makeString("[", "/", "]"), isOneOf("[2/2/1]", "[1/2/2]", "[2/1/2]"));

        StringBuilder builder1 = new StringBuilder();
        iterable.appendString(builder1);
        assertThat(builder1.toString(), isOneOf("2, 2, 1", "1, 2, 2", "2, 1, 2"));

        StringBuilder builder2 = new StringBuilder();
        iterable.appendString(builder2, "/");
        assertThat(builder2.toString(), isOneOf("2/2/1", "1/2/2", "2/1/2"));

        StringBuilder builder3 = new StringBuilder();
        iterable.appendString(builder3, "[", "/", "]");
        assertThat(builder3.toString(), isOneOf("[2/2/1]", "[1/2/2]", "[2/1/2]"));
    }

    @Override
    @Test
    default void RichIterable_toString()
    {
        String string = this.newWith(2, 2, 1).toString();
        Pattern pattern = Pattern.compile("^\\{\\d\\.\\d+=(\\d),"
                + " \\d\\.\\d+=(\\d),"
                + " \\d\\.\\d+=(\\d)\\}$");
        Matcher matcher = pattern.matcher(string);
        Assert.assertTrue(string, matcher.matches());

        assertEquals(
                Bags.immutable.with("1", "2", "2"),
                Bags.immutable.with(
                        matcher.group(1),
                        matcher.group(2),
                        matcher.group(3)));
    }

    @Override
    @Test
    default void RichIterable_toList()
    {
        assertThat(
                this.newWith(2, 2, 1).toList(),
                isOneOf(
                        Lists.immutable.with(2, 2, 1),
                        Lists.immutable.with(1, 2, 2),
                        Lists.immutable.with(2, 1, 2)));
    }
}

@@ -2928,32 +2928,40 @@
 (%22%5E%5C%5C%7B%5C%5Cd%5C%5C.%5C%5Cd+
+(E-%5C%5Cd)?
 =(%5C%5Cd),%22%0A       
@@ -2979,24 +2979,32 @@
 %22 %5C%5Cd%5C%5C.%5C%5Cd+
+(E-%5C%5Cd)?
 =(%5C%5Cd),%22%0A   
@@ -3030,16 +3030,24 @@
 d%5C%5C.%5C%5Cd+
+(E-%5C%5Cd)?
 =(%5C%5Cd)%5C%5C
@@ -3307,17 +3307,17 @@
 r.group(
-1
+2
 ),%0A     
@@ -3341,33 +3341,33 @@
   matcher.group(
-2
+4
 ),%0A             
@@ -3391,17 +3391,17 @@
 r.group(
-3
+6
 )));%0A   

Fix flickering test UnsortedMapIterableTestCase.RichIterable_toString().