From ea030342c119997133028e037c74bb57f7328027 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Wed, 4 Sep 2024 07:26:01 +0300 Subject: [PATCH] More tests --- .../core/algorithms/NodeAllocator.java | 6 +- .../astranaut/core/base/ChildDescriptor.java | 64 ++++++++++++++++++- .../core/algorithms/NodeAllocatorTest.java | 22 +++++-- 3 files changed, 82 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/NodeAllocator.java b/src/main/java/org/cqfn/astranaut/core/algorithms/NodeAllocator.java index 70a5c0a..5a0e310 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/NodeAllocator.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/NodeAllocator.java @@ -92,8 +92,9 @@ public boolean allocate(final Node[] destination, final List source) { final int count = source.size(); if (capacity == 0 && count == 0) { result = true; + } else if (destination.length < capacity) { + throw new IllegalArgumentException(); } else if (capacity >= count) { - assert destination.length == capacity; this.required = new PositionSet(false); this.required.init(); if (count >= this.required.getCount()) { @@ -128,7 +129,8 @@ private boolean fullMapping(final Node[] destination, final List source) { result = this.required.getCount() == 0; break; } - result = this.bindAllNodes(destination, array) && this.required.getCount() == 0; + result = this.bindAllNodes(destination, array); + assert !result || this.required.getCount() == 0; } while (false); return result; } diff --git a/src/main/java/org/cqfn/astranaut/core/base/ChildDescriptor.java b/src/main/java/org/cqfn/astranaut/core/base/ChildDescriptor.java index 9ceebbc..56316a5 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/ChildDescriptor.java +++ b/src/main/java/org/cqfn/astranaut/core/base/ChildDescriptor.java @@ -23,9 +23,12 @@ */ package org.cqfn.astranaut.core.base; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** * This class describes child node within type descriptor. - * * @since 1.0 */ public final class ChildDescriptor { @@ -35,7 +38,8 @@ public final class ChildDescriptor { private final String type; /** - * Flag that states that the child is optional. + * Flag that states that the child is optional, that is, one that can be in the list + * of child nodes but is not required. */ private final boolean optional; @@ -73,6 +77,15 @@ public boolean isOptional() { return this.optional; } + /** + * Creates constructor that helps build lists of descriptors. + * @return Constructor that helps build lists of descriptors + */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") + public static Constructor create() { + return new Constructor(); + } + @Override public String toString() { final String result; @@ -87,4 +100,51 @@ public String toString() { } return result; } + + /** + * Constructor that helps build lists of descriptors. + * @since 2.0.0 + */ + public static final class Constructor { + /** + * Internal list. + */ + private final List list; + + /** + * Constructor. + */ + private Constructor() { + this.list = new ArrayList<>(2); + } + + /** + * Adds a descriptor for a node that must be in the list of child nodes. + * @param type Type name of child node + * @return The constructor itself for the continuation of the chain + */ + public Constructor required(final String type) { + this.list.add(new ChildDescriptor(type, false)); + return this; + } + + /** + * Adds a descriptor for an optional node, that is, one that can be in the list + * of child nodes but is not required. + * @param type Type name of child node + * @return The constructor itself for the continuation of the chain + */ + public Constructor optional(final String type) { + this.list.add(new ChildDescriptor(type, true)); + return this; + } + + /** + * Constructs a list of descriptors. + * @return Unmodifiable list of descriptors. + */ + public List build() { + return Collections.unmodifiableList(this.list); + } + } } diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/NodeAllocatorTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/NodeAllocatorTest.java index 1460855..9da3953 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/NodeAllocatorTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/NodeAllocatorTest.java @@ -71,12 +71,9 @@ void testAllNonOptionalChildrenMapping() { @Test void testIncorrectOrderChildren() { final boolean result = this.testMapping( - Arrays.asList( - new ChildDescriptor("A", false), - new ChildDescriptor("B", false), - new ChildDescriptor("C", true), - new ChildDescriptor("D", true) - ), + ChildDescriptor.create() + .required("A").required("B").optional("C").optional("D") + .build(), Arrays.asList( new NodeDescriptor("D", 3), new NodeDescriptor("B", 1), @@ -409,6 +406,19 @@ void testEmptyDescriptions() { Assertions.assertTrue(result); } + @Test + void testBadDestination() { + final List descriptors = Arrays.asList( + new ChildDescriptor("A"), + new ChildDescriptor("B") + ); + final NodeAllocator allocator = new NodeAllocator(descriptors); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> allocator.allocate(new Node[1], Collections.emptyList()) + ); + } + /** * Common test for the {@link NodeAllocator} class. * @param types The list of types