alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (AbstractCacheTest.java)

This example Java source code file (AbstractCacheTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractcache, abstractcachetest, before, collection, exception, huffman, test, util, vocabword

The AbstractCacheTest.java Java example source code

package org.deeplearning4j.models.word2vec.wordstore.inmemory;

import org.deeplearning4j.models.word2vec.Huffman;
import org.deeplearning4j.models.word2vec.VocabWord;
import org.junit.Before;
import org.junit.Test;

import java.util.Collection;

import static org.junit.Assert.*;

/**
 * Created by fartovii on 10.12.15.
 */
public class AbstractCacheTest {

    @Before
    public void setUp() throws Exception {

    }

    @Test
    public void testNumWords() throws Exception {
        AbstractCache<VocabWord>  cache = new AbstractCache.Builder()
                .build();

        cache.addToken(new VocabWord(1.0, "word"));
        cache.addToken(new VocabWord(1.0, "test"));

        assertEquals(2, cache.numWords());
    }

    @Test
    public void testHuffman() throws Exception {
        AbstractCache<VocabWord>  cache = new AbstractCache.Builder()
                .build();

        cache.addToken(new VocabWord(1.0, "word"));
        cache.addToken(new VocabWord(2.0, "test"));
        cache.addToken(new VocabWord(3.0, "tester"));

        assertEquals(3, cache.numWords());

        Huffman huffman = new Huffman(cache.tokens());
        huffman.build();
        huffman.applyIndexes(cache);

        assertEquals("tester", cache.wordAtIndex(0));
        assertEquals("test", cache.wordAtIndex(1));
        assertEquals("word", cache.wordAtIndex(2));

        VocabWord word = cache.tokenFor("tester");
        assertEquals(0, word.getIndex());
    }

    @Test
    public void testWordsOccurencies() throws Exception {
        AbstractCache<VocabWord>  cache = new AbstractCache.Builder()
                .build();

        cache.addToken(new VocabWord(1.0, "word"));
        cache.addToken(new VocabWord(2.0, "test"));
        cache.addToken(new VocabWord(3.0, "tester"));

        assertEquals(3, cache.numWords());
        assertEquals(6, cache.totalWordOccurrences());
    }

    @Test
    public void testRemoval() throws Exception {
        AbstractCache<VocabWord>  cache = new AbstractCache.Builder()
                .build();

        cache.addToken(new VocabWord(1.0, "word"));
        cache.addToken(new VocabWord(2.0, "test"));
        cache.addToken(new VocabWord(3.0, "tester"));

        assertEquals(3, cache.numWords());
        assertEquals(6, cache.totalWordOccurrences());

        cache.removeElement("tester");
        assertEquals(2, cache.numWords());
        assertEquals(3, cache.totalWordOccurrences());
    }

    @Test
    public void testLabels() throws Exception {
        AbstractCache<VocabWord>  cache = new AbstractCache.Builder()
                .build();

        cache.addToken(new VocabWord(1.0, "word"));
        cache.addToken(new VocabWord(2.0, "test"));
        cache.addToken(new VocabWord(3.0, "tester"));

        Collection<String> collection = cache.words();
        assertEquals(3, collection.size());

        assertTrue(collection.contains("word"));
        assertTrue(collection.contains("test"));
        assertTrue(collection.contains("tester"));
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java AbstractCacheTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.