/*
 * Copyright 2012-2018 Chronicle Map Contributors
 *
 * 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 eg;

import com.google.common.io.ByteStreams;
import net.openhft.chronicle.core.io.Closeable;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.values.Values;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;

import static java.nio.charset.StandardCharsets.UTF_8;

public class WordCountTest {

    static String[] words;
    static int expectedSize;

    static {
        try {
            // english version of war and peace ->  ascii
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            InputStream zippedIS = cl.getResourceAsStream("war_and_peace.txt.gz");
            GZIPInputStream binaryIS = new GZIPInputStream(zippedIS);
            String fullText =
                    new String(ByteStreams.toByteArray(binaryIS), UTF_8);
            words = fullText.split("\\s+");
            expectedSize = (int) Arrays.stream(words).distinct().count();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void wordCountTest() throws IOException {
        try (ChronicleMap<CharSequence, IntValue> map = ChronicleMap
                .of(CharSequence.class, IntValue.class)
                .averageKeySize(7) // average word is 7 ascii bytes long (text in english)
                .entries(expectedSize)
                .create()) {
            IntValue v = Values.newNativeReference(IntValue.class);
            for (String word : words) {
                try (Closeable ignored = map.acquireContext(word, v)) {
                    v.addValue(1);
                }
            }
        }
    }
}


@@ -853,16 +853,41 @@
 Values;%0A
+import org.junit.Ignore;%0A
 import o
@@ -983,16 +983,100 @@
 Arrays;%0A
+import java.util.Map;%0Aimport java.util.Objects;%0Aimport java.util.function.Function;%0A
 import j
@@ -1160,16 +1160,167 @@
 s.UTF_8;
+%0Aimport static java.util.stream.Collectors.groupingBy;%0Aimport static java.util.stream.Collectors.reducing;%0Aimport static org.junit.Assert.assertEquals;
 %0A%0Apublic
@@ -1381,19 +1381,42 @@
  static 
-int
+Map%3CCharSequence, Integer%3E
  expecte
@@ -1416,20 +1416,19 @@
 expected
-Size
+Map
 ;%0A%0A    s
@@ -1439,26 +1439,8 @@
 c %7B%0A
-        try %7B%0A    
     
@@ -1489,20 +1489,16 @@
   ascii%0A
-    
         
@@ -1570,20 +1570,21 @@
         
-   
- 
+try (
 InputStr
@@ -1598,16 +1598,39 @@
 pedIS = 
+Objects.requireNonNull(
 cl.getRe
@@ -1667,18 +1667,20 @@
 txt.gz%22)
+)
 ;%0A
+ 
         
@@ -1740,16 +1740,19 @@
 ppedIS);
+) %7B
 %0A       
@@ -1916,20 +1916,13 @@
 cted
-Size = (int)
+Map =
  Arr
@@ -1942,26 +1942,182 @@
 rds)
-.distinct().count(
+%0A                    .collect(groupingBy(%0A                            Function.identity(),%0A                            reducing(0, e -%3E 1, Integer::sum))%0A                    
 );%0A 
@@ -2209,16 +2209,83 @@
 %0A    %7D%0A%0A
+    @Ignore(%22https://github.com/OpenHFT/Chronicle-Map/issues/376%22)%0A
     @Tes
@@ -2321,27 +2321,8 @@
 st()
- throws IOException
  %7B%0A 
@@ -2569,20 +2569,26 @@
 expected
-S
+Map.s
 ize
+()
 )%0A      
@@ -2678,16 +2678,17 @@
 class);%0A
+%0A
         
@@ -2849,32 +2849,225 @@
  %7D%0A            %7D
+%0A%0A            assertEquals(expectedMap.size(), map.size());%0A            expectedMap.forEach((key, value) -%3E%0A                    assertEquals((int) value, map.get(key).getValue())%0A            );
 %0A        %7D%0A    %7D

Improve and ignore test, Work on #376