package com.stapleslabs.tree;

import com.stapleslabs.features.IFeature;
import com.stapleslabs.utils.TestTrees;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertEquals;

public class ForestTest {

    private final List<Tree<Map<IFeature, Integer>>> trees = new ArrayList<>();
    private Map<IFeature, Integer> features;

    @Before
    public void setUp() {
        TestTrees treeGenerator = new TestTrees();
        for (int i = 0; i < 30; i ++) {
            trees.add(treeGenerator.getRandomTree());
        }
        features = treeGenerator.getRandomFeatures();

    }

    @Test
    public void testReduceToValuesProducesSameValuesAsTrees() {
        List<Double> singleResults = new ArrayList<>();

        for (Tree<Map<IFeature, Integer>> tree : trees) {
            singleResults.add(tree.reduceToValue(features));
        }

        Forest<Map<IFeature, Integer>> forest = new Forest<>(trees);

        int counter = 0;
        for (double d : forest.reduceToValues(features)) {
            assertEquals(singleResults.get(counter), d, 0.0);
            counter++;
        }
    }

    @Test
    public void testReduceToForest() {
        Set<IFeature> missingFeatures = new HashSet<>();
        missingFeatures.add(TestTrees.Feature.CLASS_ID);
        missingFeatures.add(TestTrees.Feature.COG);
        missingFeatures.add(TestTrees.Feature.MONTH);

        Forest<Map<IFeature, Integer>> forest = new Forest<>(trees);

        Forest<Map<IFeature, Integer>> subForest = forest.reduceToForest(features, missingFeatures);

        List<Double> singleResults = new ArrayList<>();

        for (Tree<Map<IFeature, Integer>> tree : trees) {
            singleResults.add(tree.reduceToValue(features));
        }

        int counter = 0;
        for (double d : subForest.reduceToValues(features)) {
            assertEquals(singleResults.get(counter), d, 0.0);
            counter++;
        }
    }

}
@@ -220,16 +220,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Apublic
@@ -1234,16 +1234,35 @@
 ToForest
+WithMissingFeatures
 () %7B%0A   
@@ -1647,24 +1647,1341 @@
 Features);%0A%0A
+        assertAccurateResults(subForest);%0A    %7D%0A%0A    @Test%0A    public void testReduceToForestWithoutMissingFeatures() %7B%0A        Set%3CIFeature%3E missingFeatures = new HashSet%3C%3E();%0A%0A        Forest%3CMap%3CIFeature, Integer%3E%3E forest = new Forest%3C%3E(trees);%0A%0A        Forest%3CMap%3CIFeature, Integer%3E%3E subForest = forest.reduceToForest(features, missingFeatures);%0A%0A        assertTrue(subForest.getNodes().length == trees.size());%0A%0A        assertAccurateResults(subForest);%0A    %7D%0A%0A    @Test%0A    public void testReduceToForestWithAllFeaturesMissing() %7B%0A        Set%3CIFeature%3E missingFeatures = new HashSet%3C%3E();%0A        missingFeatures.add(TestTrees.Feature.CLASS_ID);%0A        missingFeatures.add(TestTrees.Feature.COG);%0A        missingFeatures.add(TestTrees.Feature.MONTH);%0A        missingFeatures.add(TestTrees.Feature.DAY_OF_WEEK);%0A        missingFeatures.add(TestTrees.Feature.COST);%0A        missingFeatures.add(TestTrees.Feature.DISTANCE);%0A%0A        Forest%3CMap%3CIFeature, Integer%3E%3E forest = new Forest%3C%3E(trees);%0A%0A        Forest%3CMap%3CIFeature, Integer%3E%3E subForest = forest.reduceToForest(features, missingFeatures);%0A%0A        assertTrue(subForest.getNodes().length %3E= forest.getNodes().length);%0A%0A%0A        assertAccurateResults(subForest);%0A    %7D%0A%0A    private void assertAccurateResults(final Forest%3CMap%3CIFeature, Integer%3E%3E subForest) %7B%0A%0A
         List

Add tests for forest reduce scenarios