/*
 * Copyright 2019 Scott Logic Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.scottlogic.datahelix.generator.common.distribution;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class WeightedElementTest {
    @Test
    public void testConstructorRejectsZeroValuedWeight() {
        assertThrows(IllegalArgumentException.class, () -> new WeightedElement<String>("A string", 0.0D));
    }

    @Test
    public void testConstructorRejectsNegativeValuedWeight() {
        assertThrows(IllegalArgumentException.class, () -> new WeightedElement<>("A string", -1.0D));
    }

    @Test
    public void testConstructorRetainsWeight() {
        final double weight = 64.0D;
        WeightedElement<String> weightedElement = new WeightedElement<>("A string", weight);

        assertEquals(weight, weightedElement.weight());
    }

    @Test
    public void testParseValueParsesElement() {
        final int element = 1;
        final double weight = 1D;
        WeightedElement<Integer> weightedElement = new WeightedElement<>(element, weight);

        WeightedElement parsedElement = (WeightedElement) WeightedElement.parseValue(
            weightedElement,
            e -> Integer.toString((int) e)
        );

        assertTrue(parsedElement.element() instanceof String);
    }
}