package org.openlca.core.matrix;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.core.model.descriptors.LocationDescriptor;

public class RegFlowIndexTest {

	private long nextID = 1L;

	@Test
	public void testIt() {
		RegFlowIndex idx = new RegFlowIndex();
		List<FlowDescriptor> flows = new ArrayList<>();
		List<LocationDescriptor> locations = new ArrayList<>();
		List<Boolean> isInput = new ArrayList<>();

		// build the index
		boolean b = false;
		for (int i = 0; i < 10_000; i++) {
			FlowDescriptor flow = randFlow();
			flows.add(flow);
			LocationDescriptor loc = randLocation();
			locations.add(loc);
			isInput.add(b);

			Assert.assertFalse(idx.contains(flow.id, loc.id));
			Assert.assertFalse(idx.contains(flow, loc));
			int j = b
					? idx.putInput(flow, loc)
					: idx.putOutput(flow, loc);
			Assert.assertEquals(i, j);
			b = !b;
		}

		// check the index
		for (int i = 0; i < 10_000; i++) {
			FlowDescriptor flow = idx.flowAt(i);
			Assert.assertEquals(flows.get(i), flow);
			LocationDescriptor loc = idx.locationAt(i);
			Assert.assertEquals(locations.get(i), loc);

			Assert.assertEquals(i, idx.of(flow.id, loc.id));
			Assert.assertEquals(i, idx.of(flow, loc));
			Assert.assertTrue(idx.contains(flow.id, loc.id));
			Assert.assertTrue(idx.contains(flow, loc));
			Assert.assertEquals(isInput.get(i), idx.isInput(flow.id));
			Assert.assertEquals(isInput.get(i), idx.isInput(flow));
		}
	}

	private FlowDescriptor randFlow() {
		FlowDescriptor flow = new FlowDescriptor();
		flow.id = nextID++;
		flow.name = "Flow " + flow.id;
		return flow;
	}

	private LocationDescriptor randLocation() {
		LocationDescriptor loc = new LocationDescriptor();
		loc.id = nextID++;
		loc.name = "Location " + loc.id;
		return loc;
	}
}

@@ -31,60 +31,8 @@
 x;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 impo
@@ -196,16 +196,68 @@
 iptor;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 public c
@@ -1543,24 +1543,920 @@
 ));%0A%09%09%7D%0A%09%7D%0A%0A
+%09@Test%0A%09public void testNonRegFlows() %7B%0A%0A%09%09RegFlowIndex idx = new RegFlowIndex();%0A%0A%09%09// add 500 flows with location and 500 without%0A%09%09boolean isInput = true;%0A%09%09for (int i = 0; i %3C 1000; i++) %7B%0A%09%09%09if (i %25 5 == 0) %7B%0A%09%09%09%09isInput = !isInput;%0A%09%09%09%7D%0A%09%09%09if (i %25 2 == 0) %7B%0A%09%09%09%09int _i = isInput%0A%09%09%09%09%09%09? idx.putInput(randFlow())%0A%09%09%09%09%09%09: idx.putOutput(randFlow());%0A%09%09%09%09Assert.assertEquals(i, _i);%0A%09%09%09%7D else %7B%0A%09%09%09%09int _i = isInput%0A%09%09%09%09%09%09? idx.putInput(randFlow(), randLocation())%0A%09%09%09%09%09%09: idx.putOutput(randFlow(), randLocation());%0A%09%09%09%09Assert.assertEquals(i, _i);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09// check the index%0A%09%09Assert.assertEquals(1000, idx.getFlows().size());%0A%09%09Assert.assertEquals(500, idx.getLocations().size());%0A%09%09for (int i = 0; i %3C 1000; i++) %7B%0A%09%09%09Assert.assertNotNull(idx.flowAt(i));%0A%09%09%09if (i %25 2 == 0) %7B%0A%09%09%09%09Assert.assertNull(idx.locationAt(i));%0A%09%09%09%7D else %7B%0A%09%09%09%09Assert.assertNotNull(idx.locationAt(i));%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %09private Flo

test that out RegFlowIndex can handle non-regionalized flows