Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Sep 4, 2024
1 parent 525e1c7 commit ea03034
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ public boolean allocate(final Node[] destination, final List<Node> 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()) {
Expand Down Expand Up @@ -128,7 +129,8 @@ private boolean fullMapping(final Node[] destination, final List<Node> 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;
}
Expand Down
64 changes: 62 additions & 2 deletions src/main/java/org/cqfn/astranaut/core/base/ChildDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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<ChildDescriptor> 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<ChildDescriptor> build() {
return Collections.unmodifiableList(this.list);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -409,6 +406,19 @@ void testEmptyDescriptions() {
Assertions.assertTrue(result);
}

@Test
void testBadDestination() {
final List<ChildDescriptor> 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
Expand Down

0 comments on commit ea03034

Please sign in to comment.