<commit_before>package net.openhft.chronicle.map;

import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.io.serialization.impl.*;
import net.openhft.lang.values.*;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.ExecutionException;

import static org.junit.Assert.*;

/**
 * This test enumerates common usecases for keys and values.
 */
// TODO Test for persisted map.
// TODO Test for stateless map.

public class CHMUseCasesTest {
    /**
     * String is not as efficient as CharSequence as a key or value but easier to use The key can
     * only be on heap and variable length serialised.
     */
    @Test
    public void testStringStringMap() throws ExecutionException, InterruptedException {
/* TODO run the same test for multiple types of map stores
        for(ChronicleMapBuilder<String, String> chmb : Arrays.asList(
                ChronicleMapBuilder.of(String.class, String.class),
                ChronicleMapBuilder.of(String.class, String.class).file(TMP_FILE),
                ChronicleMapBuilder.of(String.class, String.class).statelessClient(CONFIG)
        )) {
            try (ChronicleMap<String, String> map = chmb.create()) {
*/
        try (ChronicleMap<String, String> map = ChronicleMapBuilder
                .of(String.class, String.class) // for testing purposes only
                .create()) {
            map.put("Hello", "World");
            assertEquals("World", map.get("Hello"));

            assertEquals("New World", map.getMapped("Hello", new Function<String, String>() {
                @Override
                public String apply(String s) {
                    return "New " + s;
                }
            }));
            assertEquals(null, map.getMapped("No key", new Function<String, String>() {
                @Override
                public String apply(String s) {
                    return "New " + s;
                }
            }));


        }
    }


    @Test
    public void testStringStringMapMutableValue() throws ExecutionException, InterruptedException {
/* TODO run the same test for multiple types of map stores
        for(ChronicleMapBuilder<String, String> chmb : Arrays.asList(
                ChronicleMapBuilder.of(String.class, String.class),
                ChronicleMapBuilder.of(String.class, String.class).file(TMP_FILE),
                ChronicleMapBuilder.of(String.class, String.class).statelessClient(CONFIG)
        )) {
            try (ChronicleMap<String, String> map = chmb.create()) {
*/
        try (ChronicleMap<String, String> map = ChronicleMapBuilder
                .of(String.class, String.class) // for testing purposes only
                .create()) {
            map.put("Hello", "World");


            map.putMapped("Hello", new UnaryOperator<String>() {
                @Override
                public String update(String s) {
                    return "New " + s;
                }
            });
        }
    }

    /**
     * CharSequence is more efficient when object creation is avoided. The key can only be on heap
     * and variable length serialised.
     */
    @Test
    public void testCharSequenceCharSequenceMap() throws ExecutionException, InterruptedException {
        try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
                .of(CharSequence.class, CharSequence.class) // for testing purposes only
                .create()) {<commit_msg>added tests<commit_after>package net.openhft.chronicle.map;

import junit.framework.Assert;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.io.serialization.impl.*;
import net.openhft.lang.values.*;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.ExecutionException;

import static org.junit.Assert.*;

/**
 * This test enumerates common usecases for keys and values.
 */
// TODO Test for persisted map.
// TODO Test for stateless map.

public class CHMUseCasesTest {
    /**
     * String is not as efficient as CharSequence as a key or value but easier to use The key can
     * only be on heap and variable length serialised.
     */
    @Test
    public void testStringStringMap() throws ExecutionException, InterruptedException {
/* TODO run the same test for multiple types of map stores
        for(ChronicleMapBuilder<String, String> chmb : Arrays.asList(
                ChronicleMapBuilder.of(String.class, String.class),
                ChronicleMapBuilder.of(String.class, String.class).file(TMP_FILE),
                ChronicleMapBuilder.of(String.class, String.class).statelessClient(CONFIG)
        )) {
            try (ChronicleMap<String, String> map = chmb.create()) {
*/
        try (ChronicleMap<String, String> map = ChronicleMapBuilder
                .of(String.class, String.class) // for testing purposes only
                .create()) {
            map.put("Hello", "World");
            assertEquals("World", map.get("Hello"));

            assertEquals("New World", map.getMapped("Hello", new Function<String, String>() {
                @Override
                public String apply(String s) {
                    return "New " + s;
                }
            }));
            assertEquals(null, map.getMapped("No key", new Function<String, String>() {
                @Override
                public String apply(String s) {
                    return "New " + s;
                }
            }));


        }
    }


    @Test
    public void testStringStringMapMutableValue() throws ExecutionException, InterruptedException {
/* TODO run the same test for multiple types of map stores
        for(ChronicleMapBuilder<String, String> chmb : Arrays.asList(
                ChronicleMapBuilder.of(String.class, String.class),
                ChronicleMapBuilder.of(String.class, String.class).file(TMP_FILE),
                ChronicleMapBuilder.of(String.class, String.class).statelessClient(CONFIG)
        )) {
            try (ChronicleMap<String, String> map = chmb.create()) {
*/
        try (ChronicleMap<String, String> map = ChronicleMapBuilder
                .of(String.class, String.class) // for testing purposes only
                .create()) {
            map.put("Hello", "World");


            map.putMapped("Hello", new UnaryOperator<String>() {
                @Override
                public String update(String s) {
                    return "New " + s;
                }
            });
        }
    }

    @Test
    public void testCharSequenceMixingKeyTypes() throws ExecutionException, InterruptedException {

        try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
                .of(CharSequence.class, CharSequence.class) // for testing purposes only
                .create()) {
            map.put("Hello", "World");
            map.put(new StringBuilder("Hello"), "World2");

            Assert.assertEquals("World2", map.get("Hello"));
        }
    }

    @Test
    public void testCharSequenceMixingValueTypes() throws ExecutionException, InterruptedException {

        try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
                .of(CharSequence.class, CharSequence.class) // for testing purposes only
                .create()) {
            map.put("Hello", "World");
            map.put("Hello2", new StringBuilder("World2"));

            Assert.assertEquals("World2", map.get("Hello2"));
            Assert.assertEquals("World", map.get("Hello"));
        }
    }


    /**
     * CharSequence is more efficient when object creation is avoided. The key can only be on heap
     * and variable length serialised.
     */
    @Test
    public void testCharSequenceCharSequenceMap() throws ExecutionException, InterruptedException {
        try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
                .of(CharSequence.class, CharSequence.class) // for testing purposes only
                .create()) {
{'fraction_non_alphanumeric': 0.10464250248262165, 'fraction_numerical': 0.0007447864945382324, 'mean_word_length': 2.239646160032167, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3748712', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2061, 'n_words': 545}