package org.neuralnetwork.neuron;

import static org.junit.Assert.*;

import org.junit.Test;
import org.neuralnetwork.functions.AbstractActivationFunction;
import org.neuralnetwork.functions.HeavisideFunction;
import org.neuralnetwork.functions.SigmoidFunction;
import org.neuralnetwork.synapse.AbstractWeightProductSynapse;
import org.neuralnetwork.synapse.AbstractWeightedSynapse;

public class DefaultNeuronTest {

	@Test
	public void testBiasSynapse() {
		DefaultNeuron neuron = new DefaultNeuron(0.0);
		AbstractWeightProductSynapse<Double> biasSynapse = neuron
				.getBiasSynapse();
		assertEquals(0, biasSynapse.getWeight(), 0);
		biasSynapse.setWeight(1.0);
		assertEquals(-1, biasSynapse.getValue(), 0);
	}

	@Test
	public void testSetGetActivationFunction() {
		DefaultNeuron neuron = new DefaultNeuron(0.0);
		AbstractActivationFunction<Double, Double> function = neuron
				.getActivationFunction();
		assertTrue(function instanceof SigmoidFunction);
		assertEquals(1, ((SigmoidFunction) function).getLambda(), 0);

		function = new HeavisideFunction();
		neuron.setActivationFunction(function);
		assertEquals(function, neuron.getActivationFunction());

		function = new AbstractActivationFunction<Double, Double>() {
			@Override
			public Double compute(Double input) {
				return input;
			}
		};
		neuron.setActivationFunction(function);
		assertEquals(function, neuron.getActivationFunction());
	}

	@Test
	public void testHeavisideActivationFunctionValue() {
		DefaultNeuron neuron = new DefaultNeuron(0.0);
		neuron.setActivationFunction(new HeavisideFunction());
		InputTest input = new InputTest();
		neuron.addSynapse(new AbstractWeightedSynapse<InputTest, Double, Double>(
				input, 1.0) {
			@Override
			protected Double readValueFrom(InputTest input, Double weight) {
				return input.value;
			}
		});

		input.value = 0;
		neuron.computeValue();
		assertEquals(1, neuron.getValue(), 0);

		input.value = -1;
		neuron.computeValue();
		assertEquals(0, neuron.getValue(), 0);

		input.value = +1;
		neuron.computeValue();
		assertEquals(1, neuron.getValue(), 0);
	}

	@Test
	public void testSigmoidActivationFunctionValue() {
		DefaultNeuron neuron = new DefaultNeuron(0.0);
		neuron.setActivationFunction(new AbstractActivationFunction<Double, Double>() {
			@Override
			public Double compute(Double input) {
				return input > 1.0 && input < 2.0 ? 1.0 : 0.0;
			}
		});
		InputTest input = new InputTest();
		neuron.addSynapse(new AbstractWeightProductSynapse<InputTest>(input, 2.0) {
			@Override
			protected Double getValueFromInput(InputTest input) {
				return input.value;
			}
		});

		input.value = -50;
		neuron.computeValue();
		assertEquals(0, neuron.getValue(), 0);

		input.value = 0;
		neuron.computeValue();
		assertEquals(0, neuron.getValue(), 0);

		input.value = 0.4;
		neuron.computeValue();
		assertEquals(0, neuron.getValue(), 0);

		input.value = 0.5;
		neuron.computeValue();
		assertEquals(0, neuron.getValue(), 0);

		input.value = 0.6;
		neuron.computeValue();
		assertEquals(1, neuron.getValue(), 0);

		input.value = 0.9;
		neuron.computeValue();
		assertEquals(1, neuron.getValue(), 0);

		input.value = 1;
		neuron.computeValue();
		assertEquals(0, neuron.getValue(), 0);
	}

	class InputTest {
		double value = 0;
	}
}

@@ -321,66 +321,8 @@
 pse;
-%0Aimport org.neuralnetwork.synapse.AbstractWeightedSynapse;
 %0A%0Apu
@@ -1381,693 +1381,8 @@
 test
-HeavisideActivationFunctionValue() %7B%0A%09%09DefaultNeuron neuron = new DefaultNeuron(0.0);%0A%09%09neuron.setActivationFunction(new HeavisideFunction());%0A%09%09InputTest input = new InputTest();%0A%09%09neuron.addSynapse(new AbstractWeightedSynapse%3CInputTest, Double, Double%3E(%0A%09%09%09%09input, 1.0) %7B%0A%09%09%09@Override%0A%09%09%09protected Double readValueFrom(InputTest input, Double weight) %7B%0A%09%09%09%09return input.value;%0A%09%09%09%7D%0A%09%09%7D);%0A%0A%09%09input.value = 0;%0A%09%09neuron.computeValue();%0A%09%09assertEquals(1, neuron.getValue(), 0);%0A%0A%09%09input.value = -1;%0A%09%09neuron.computeValue();%0A%09%09assertEquals(0, neuron.getValue(), 0);%0A%0A%09%09input.value = +1;%0A%09%09neuron.computeValue();%0A%09%09assertEquals(1, neuron.getValue(), 0);%0A%09%7D%0A%0A%09@Test%0A%09public void testSigmoid
 Acti
@@ -1759,17 +1759,21 @@
 %3E(input,
- 
+%0A%09%09%09%09
 2.0) %7B%0A%09

Remove specific activation functions test (Heaviside) and fix custom activation function test name.