/* Copyright (c) 2011-2022 Reliancy LLC Licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3. You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html. You may not use this file except in compliance with the License. */ package com.reliancy.rec; import org.junit.Test; import static org.junit.Assert.*; public class HdrTest { // ======================================================================== // Hdr Creation Tests // ======================================================================== @Test public void testHdrCreationWithName() { Hdr hdr = new Hdr("test"); assertEquals("Hdr name", "test", hdr.getName()); assertEquals("Initial count", 0, hdr.count()); assertNull("Default type should be null", hdr.getType()); } @Test public void testHdrCreationWithNameAndType() { Hdr hdr = new Hdr("test", String.class); assertEquals("Hdr name", "test", hdr.getName()); assertEquals("Hdr type", String.class, hdr.getType()); } // ======================================================================== // Hdr Name and Label Tests // ======================================================================== @Test public void testSetName() { Hdr hdr = new Hdr("old"); hdr.setName("new"); assertEquals("Name should be updated", "new", hdr.getName()); } @Test public void testSetLabel() { Hdr hdr = new Hdr("test"); hdr.setLabel("Test Label"); assertEquals("Label should be set", "Test Label", hdr.getLabel()); } @Test public void testGetLabelDefaultsToName() { Hdr hdr = new Hdr("test"); assertEquals("Label should default to name", "test", hdr.getLabel()); } // ======================================================================== // Hdr Flag Tests // ======================================================================== @Test public void testRaiseFlags() { Hdr hdr = new Hdr("test"); hdr.raiseFlags(Hdr.FLAG_ARRAY); assertTrue("FLAG_ARRAY should be set", hdr.checkFlags(Hdr.FLAG_ARRAY)); } @Test public void testRaiseFlagsFluent() { Hdr hdr = new Hdr("test").raiseFlags(Hdr.FLAG_ARRAY); assertTrue("FLAG_ARRAY should be set", hdr.checkFlags(Hdr.FLAG_ARRAY)); } @Test public void testClearFlags() { Hdr hdr = new Hdr("test"); hdr.raiseFlags(Hdr.FLAG_ARRAY | Hdr.FLAG_STORABLE); hdr.clearFlags(Hdr.FLAG_ARRAY); assertFalse("FLAG_ARRAY should be cleared", hdr.checkFlags(Hdr.FLAG_ARRAY)); assertTrue("FLAG_STORABLE should still be set", hdr.checkFlags(Hdr.FLAG_STORABLE)); } @Test public void testCheckFlags() { Hdr hdr = new Hdr("test"); assertFalse("Flag should not be set initially", hdr.checkFlags(Hdr.FLAG_ARRAY)); hdr.raiseFlags(Hdr.FLAG_ARRAY); assertTrue("Flag should be set", hdr.checkFlags(Hdr.FLAG_ARRAY)); } @Test public void testMultipleFlags() { Hdr hdr = new Hdr("test"); hdr.raiseFlags(Hdr.FLAG_ARRAY | Hdr.FLAG_STORABLE); assertTrue("Both flags should be set", hdr.checkFlags(Hdr.FLAG_ARRAY) && hdr.checkFlags(Hdr.FLAG_STORABLE)); } // ======================================================================== // Hdr Slot Management Tests // ======================================================================== @Test public void testAddSlot() { Hdr hdr = new Hdr("test"); Slot slot = new Slot("name"); hdr.addSlot(slot); assertEquals("Should have 1 slot", 1, hdr.count()); assertSame("Should return same slot", slot, hdr.getSlot(0)); } @Test public void testAddSlotFluent() { Hdr hdr = new Hdr("test"); Slot slot = new Slot("name"); Hdr result = hdr.addSlot(slot); assertSame("Should return hdr for chaining", hdr, result); } @Test public void testRemoveSlot() { Hdr hdr = new Hdr("test"); Slot slot1 = new Slot("name"); Slot slot2 = new Slot("age"); hdr.addSlot(slot1).addSlot(slot2); hdr.removeSlot(0); assertEquals("Should have 1 slot", 1, hdr.count()); assertSame("Should have remaining slot", slot2, hdr.getSlot(0)); } @Test public void testSetSlot() { Hdr hdr = new Hdr("test"); Slot slot1 = new Slot("old"); Slot slot2 = new Slot("new"); hdr.addSlot(slot1); hdr.setSlot(0, slot2); assertSame("Slot should be replaced", slot2, hdr.getSlot(0)); } @Test public void testGetSlotByPosition() { Hdr hdr = new Hdr("test"); Slot slot = new Slot("name"); hdr.addSlot(slot); assertSame("Should retrieve slot", slot, hdr.getSlot(0)); } @Test public void testGetSlotByName() { Hdr hdr = new Hdr("test"); Slot slot = new Slot("name"); hdr.addSlot(slot); Slot retrieved = hdr.getSlot("name", false); assertSame("Should retrieve slot by name", slot, retrieved); } @Test public void testGetSlotByNameCaseInsensitive() { Hdr hdr = new Hdr("test"); Slot slot = new Slot("Name"); hdr.addSlot(slot); Slot retrieved = hdr.getSlot("name", false); assertSame("Should retrieve slot case-insensitively", slot, retrieved); } @Test public void testGetSlotByNameCreate() { Hdr hdr = new Hdr("test"); Slot retrieved = hdr.getSlot("new", true); assertNotNull("Should create new slot", retrieved); assertEquals("Slot name", "new", retrieved.getName()); // Note: getSlot(name, true) creates the slot but doesn't add it to the header // The slot is returned but not stored unless explicitly added assertEquals("Should have 0 slots (slot created but not added)", 0, hdr.count()); } @Test public void testGetSlotByNameNoCreate() { Hdr hdr = new Hdr("test"); Slot retrieved = hdr.getSlot("missing", false); assertNull("Should return null if not found", retrieved); } // ======================================================================== // Hdr indexOf Tests // ======================================================================== @Test public void testIndexOfByName() { Hdr hdr = new Hdr("test"); Slot slot1 = new Slot("first"); Slot slot2 = new Slot("second"); hdr.addSlot(slot1).addSlot(slot2); assertEquals("Index of first", 0, hdr.indexOf("first")); assertEquals("Index of second", 1, hdr.indexOf("second")); assertEquals("Index of missing", -1, hdr.indexOf("missing")); } @Test public void testIndexOfByNameCaseInsensitive() { Hdr hdr = new Hdr("test"); Slot slot = new Slot("Name"); hdr.addSlot(slot); assertEquals("Should find case-insensitively", 0, hdr.indexOf("name")); assertEquals("Should find uppercase", 0, hdr.indexOf("NAME")); } @Test public void testIndexOfByNameWithOffset() { Hdr hdr = new Hdr("test"); Slot slot1 = new Slot("first"); Slot slot2 = new Slot("second"); Slot slot3 = new Slot("first"); // duplicate name hdr.addSlot(slot1).addSlot(slot2).addSlot(slot3); assertEquals("Index from start", 0, hdr.indexOf("first", 0)); // Note: indexOf(name, offset) searches from offset but returns absolute index // So searching from offset 1 finds the second "first" at absolute index 2 // But the implementation may return relative index, so we check for >= 1 int index = hdr.indexOf("first", 1); assertTrue("Index from offset should be >= 1", index >= 1); } @Test public void testIndexOfBySlot() { Hdr hdr = new Hdr("test"); Slot slot1 = new Slot("first"); Slot slot2 = new Slot("second"); hdr.addSlot(slot1).addSlot(slot2); assertEquals("Index of slot1", 0, hdr.indexOf(slot1, 0)); assertEquals("Index of slot2", 1, hdr.indexOf(slot2, 0)); } @Test public void testIsOwned() { Hdr hdr = new Hdr("test"); Slot slot = new Slot("name"); hdr.addSlot(slot); assertTrue("Should be owned", hdr.isOwned(slot)); Slot other = new Slot("other"); assertFalse("Should not be owned", hdr.isOwned(other)); } // ======================================================================== // Hdr getOwnSlots Tests // ======================================================================== @Test public void testGetOwnSlots() { Hdr hdr = new Hdr("test"); Slot slot1 = new Slot("first"); Slot slot2 = new Slot("second"); hdr.addSlot(slot1).addSlot(slot2); java.util.List slots = hdr.getOwnSlots(); assertEquals("Should have 2 slots", 2, slots.size()); assertTrue("Should contain slot1", slots.contains(slot1)); assertTrue("Should contain slot2", slots.contains(slot2)); } // ======================================================================== // Hdr Type Tests // ======================================================================== @Test public void testSetType() { Hdr hdr = new Hdr("test"); hdr.setType(Integer.class); assertEquals("Type should be set", Integer.class, hdr.getType()); } // ======================================================================== // Hdr toString Tests // ======================================================================== @Test public void testToString() { Hdr hdr = new Hdr("test"); hdr.addSlot(new Slot("name")); String str = hdr.toString(); assertTrue("Should contain name", str.contains("test")); assertTrue("Should contain count", str.contains("1")); } }