private void checkWriterParent(TaxonomyReader tr, TaxonomyWriter tw) throws Exception { // check that the parent of the root ordinal is the invalid ordinal: assertEquals(TaxonomyReader.INVALID_ORDINAL, tw.getParent(0)); // check parent of non-root ordinals: for (int ordinal = 1; ordinal < tr.getSize(); ordinal++) { FacetLabel me = tr.getPath(ordinal); int parentOrdinal = tw.getParent(ordinal); FacetLabel parent = tr.getPath(parentOrdinal); if (parent == null) { fail( "Parent of " + ordinal + " is " + parentOrdinal + ", but this is not a valid category."); } // verify that the parent is indeed my parent, according to the // strings if (!me.subpath(me.length - 1).equals(parent)) { fail( "Got parent " + parentOrdinal + " for ordinal " + ordinal + " but categories are " + showcat(parent) + " and " + showcat(me) + " respectively."); } } // check parent of of invalid ordinals: try { tw.getParent(-1); fail("getParent for -1 should throw exception"); } catch (ArrayIndexOutOfBoundsException e) { // ok } try { tw.getParent(TaxonomyReader.INVALID_ORDINAL); fail("getParent for INVALID_ORDINAL should throw exception"); } catch (ArrayIndexOutOfBoundsException e) { // ok } try { int parent = tw.getParent(tr.getSize()); fail("getParent for getSize() should throw exception, but returned " + parent); } catch (ArrayIndexOutOfBoundsException e) { // ok } }
/** * fillTaxonomyCheckPaths adds the categories in the categories[] array, and asserts that the * additions return exactly paths specified in expectedPaths[]. This is the same add * fillTaxonomy() but also checks the correctness of getParent(), not just addCategory(). Note * that this assumes that fillTaxonomyCheckPaths() is called on an empty taxonomy index. Calling * it after something else was already added to the taxonomy index will surely have this method * fail. */ public static void fillTaxonomyCheckPaths(TaxonomyWriter tw) throws IOException { for (int i = 0; i < categories.length; i++) { int ordinal = tw.addCategory(new FacetLabel(categories[i])); int expectedOrdinal = expectedPaths[i][expectedPaths[i].length - 1]; if (ordinal != expectedOrdinal) { fail( "For category " + showcat(categories[i]) + " expected ordinal " + expectedOrdinal + ", but got " + ordinal); } for (int j = expectedPaths[i].length - 2; j >= 0; j--) { ordinal = tw.getParent(ordinal); expectedOrdinal = expectedPaths[i][j]; if (ordinal != expectedOrdinal) { fail( "For category " + showcat(categories[i]) + " expected ancestor level " + (expectedPaths[i].length - 1 - j) + " was " + expectedOrdinal + ", but got " + ordinal); } } } }
// After fillTaxonomy returned successfully, checkPaths() checks that // the getParent() calls return as expected, from the table public static void checkPaths(TaxonomyWriter tw) throws IOException { for (int i = 0; i < categories.length; i++) { int ordinal = expectedPaths[i][expectedPaths[i].length - 1]; for (int j = expectedPaths[i].length - 2; j >= 0; j--) { ordinal = tw.getParent(ordinal); int expectedOrdinal = expectedPaths[i][j]; if (ordinal != expectedOrdinal) { fail( "For category " + showcat(categories[i]) + " expected ancestor level " + (expectedPaths[i].length - 1 - j) + " was " + expectedOrdinal + ", but got " + ordinal); } } assertEquals(TaxonomyReader.ROOT_ORDINAL, tw.getParent(expectedPaths[i][0])); } assertEquals(TaxonomyReader.INVALID_ORDINAL, tw.getParent(TaxonomyReader.ROOT_ORDINAL)); }