Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions bjforth/src/main/java/bjforth/machine/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ class Dictionary {

public void put(String name, DictionaryItem item) {
List<DictionaryItem> currentValue;
if (items.containsKey(name)) {
currentValue = items.get(name);
var nameUpper = name.toUpperCase();
if (items.containsKey(nameUpper)) {
currentValue = items.get(nameUpper);
} else {
currentValue = new ArrayList<>();
}
currentValue.add(item);
items.put(name, currentValue);
items.put(nameUpper, currentValue);
reverseLookup.putIfAbsent(item.getAddress(), item.getName());
}

public Optional<DictionaryItem> get(String name) {
return Optional.ofNullable(items.get(name))
return Optional.ofNullable(items.get(name.toUpperCase()))
.map(dictionaryItems -> dictionaryItems.get(dictionaryItems.size() - 1));
}

Expand Down
43 changes: 43 additions & 0 deletions bjforth/src/test/java/bjforth/machine/DictionaryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 Bahman Movaqar
*
* This file is part of bjForth.
*
* bjForth is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* bjForth is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with bjForth. If not, see <https://www.gnu.org/licenses/>.
*/
package bjforth.machine;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class DictionaryTest {

@DisplayName("#get() should do a case-insensitive search")
@Test
void getCaseInsensitive() {
// GIVEN
var dict = new Dictionary();
var name = RandomStringUtils.insecure().next(20);
var dictItem = new DictionaryItem(name, 0, false, false);
dict.put(name, dictItem);

// EXPECT
assertThat(dict.get(name.toLowerCase())).isPresent().hasValueSatisfying(dictItem::equals);
assertThat(dict.get(name.toUpperCase())).isPresent().hasValueSatisfying(dictItem::equals);
}
}