Skip to content

Add JUnit 5 support and initial unit test for Tokenizer #26

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/test/java/com/example/tokenizer/impl/TokenizerTest.java
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TokenizerTest covers the static replaceControlCharacters() methods which are implemented in the Tokenizer interface, not LlamaTokenizer. In order this junit support to make sense I would suggest to add coverage for LlamaTokenizer as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.tokenizer.impl;

import com.example.core.types.Pair;
import com.example.tokenizer.vocabulary.Vocabulary;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

class TokenizerTest {

private Tokenizer tokenizer;

@BeforeEach
void setup() {
String[] tokens = {"H", "e", "l", "o", " ", "He", "lo"};
float[] scores = new float[tokens.length];

// Create token to index mapping
Vocabulary vocab = new Vocabulary(tokens, scores);

List<Pair<Integer, Integer>> merges = List.of(
new Pair<>(0, 1), // H + e → He
new Pair<>(2, 3) // l + o → lo
);

String regex = "[A-Za-z ]+";

Map<String, Integer> specialTokens = Map.of("<PAD>", 100, "<EOS>", 101);

tokenizer = new Tokenizer(vocab, merges, regex, specialTokens);
}

@Test
void testEncodeOrdinary() {
List<Integer> result = tokenizer.encodeOrdinary("Hello");
assertNotNull(result);
assertTrue(result.contains(5));
assertTrue(result.contains(6));
}

@Test
void testEncodeWithSpecialToken() {
String input = "Hello<EOS>";
List<Integer> result = tokenizer.encode(input, Set.of("<EOS>"));
assertTrue(result.contains(2));
}

@Test
void testRegexPattern() {
assertEquals("[A-Za-z ]+", tokenizer.regexPattern());
}

@Test
void testDecode() {
String input = "He lo";
List<Integer> ids = tokenizer.encodeOrdinary(input);
String decoded = tokenizer.decodeImpl(ids);
assertEquals("He lo", decoded);
}

@Test
void testSpecialTokenCheck() {
assertTrue(tokenizer.isSpecialToken(100));
assertFalse(tokenizer.isSpecialToken(999));
}
}