package io.lp0onfire.smtnes;

import io.lp0onfire.smtnes.smt2.IndexedIdentifier;
import io.lp0onfire.smtnes.smt2.SExpression;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import static org.junit.Assert.*;

public class TestStateVariableRegistry {
  
  @Test
  public void testWriteOnly() {
    StateVariableRegistry reg = new StateVariableRegistry();
    CodeGenerator gen  = new CodeGenerator(){
      @Override
      public List<String> getStateVariablesRead() {
        return new LinkedList<>();
      }

      @Override
      public List<String> getStateVariablesWritten() {
        return Arrays.asList("foo");
      }

      @Override
      public List<SExpression> generateCode(Map<String, IndexedIdentifier> inputs,
          Map<String, IndexedIdentifier> outputs) {
        // we expect to receive no inputs
        assertTrue("received unexpected input", inputs.isEmpty());
        // we expect to receive one output, called "foo"
        assertTrue("received wrong number of outputs", outputs.size() == 1);
        // check that we got (_ foo 0)
        IndexedIdentifier foo = outputs.get("foo");
        assertEquals("output 'foo' has wrong symbol name", "foo", foo.getSymbol().getName());
        assertEquals("output 'foo' has wrong index", "0", foo.getIndices().get(0).getDigits());
        return new LinkedList<>();
      }
    };
    reg.apply(gen);
  }
  
}

@@ -1451,10 +1451,2507 @@
 %0A  %7D%0A  %0A
+  @Test(expected=IllegalStateException.class)%0A  public void testReadBeforeWrite_Illegal() %7B%0A    StateVariableRegistry reg = new StateVariableRegistry();%0A    CodeGenerator gen  = new CodeGenerator()%7B%0A      @Override%0A      public List%3CString%3E getStateVariablesRead() %7B%0A        return Arrays.asList(%22foo%22);%0A      %7D%0A%0A      @Override%0A      public List%3CString%3E getStateVariablesWritten() %7B%0A        return new LinkedList%3C%3E();%0A      %7D%0A%0A      @Override%0A      public List%3CSExpression%3E generateCode(Map%3CString, IndexedIdentifier%3E inputs,%0A          Map%3CString, IndexedIdentifier%3E outputs) %7B%0A        return new LinkedList%3C%3E();%0A      %7D%0A    %7D;%0A    reg.apply(gen);%0A  %7D%0A  %0A  @Test%0A  public void testReadAfterWrite() %7B%0A    StateVariableRegistry reg = new StateVariableRegistry();%0A    CodeGenerator init = new CodeGenerator() %7B%0A%0A      @Override%0A      public List%3CString%3E getStateVariablesRead() %7B%0A        return new LinkedList%3C%3E();%0A      %7D%0A%0A      @Override%0A      public List%3CString%3E getStateVariablesWritten() %7B%0A        return Arrays.asList(%22foo%22);%0A      %7D%0A%0A      @Override%0A      public List%3CSExpression%3E generateCode(%0A          Map%3CString, IndexedIdentifier%3E inputs,%0A          Map%3CString, IndexedIdentifier%3E outputs) %7B%0A        return new LinkedList%3C%3E();%0A      %7D%0A      %0A    %7D;%0A    CodeGenerator update = new CodeGenerator() %7B%0A%0A      @Override%0A      public List%3CString%3E getStateVariablesRead() %7B%0A        return Arrays.asList(%22foo%22);%0A      %7D%0A%0A      @Override%0A      public List%3CString%3E getStateVariablesWritten() %7B%0A        return Arrays.asList(%22foo%22);%0A      %7D%0A%0A      @Override%0A      public List%3CSExpression%3E generateCode(%0A          Map%3CString, IndexedIdentifier%3E inputs,%0A          Map%3CString, IndexedIdentifier%3E outputs) %7B%0A        // we should see input: (_ foo 0) and output: (_ foo 1)%0A        %0A        assertTrue(inputs.containsKey(%22foo%22));%0A        IndexedIdentifier foo_in = inputs.get(%22foo%22);%0A        assertEquals(%22input 'foo' has wrong symbol name%22, %22foo%22, foo_in.getSymbol().getName());%0A        assertEquals(%22input 'foo' has wrong index%22, %220%22, foo_in.getIndices().get(0).getDigits());%0A        %0A        assertTrue(outputs.containsKey(%22foo%22));%0A        IndexedIdentifier foo_out = outputs.get(%22foo%22);%0A        assertEquals(%22output 'foo' has wrong symbol name%22, %22foo%22, foo_out.getSymbol().getName());%0A        assertEquals(%22output 'foo' has wrong index%22, %221%22, foo_out.getIndices().get(0).getDigits());%0A        %0A        return new LinkedList%3C%3E();%0A      %7D%0A      %0A    %7D;%0A    reg.apply(init);%0A    reg.apply(update);%0A  %7D%0A  %0A
 %7D%0A

complete unit tests for state variable registry