package testcases.very.compact;

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import de.jpaw.bonaparte.core.BonaPortable;
import de.jpaw.bonaparte.core.CompactByteArrayComposer;
import de.jpaw.bonaparte.core.CompactByteArrayParser;
import de.jpaw.bonaparte.pojos.very.compact.Complex;
import de.jpaw.bonaparte.pojos.very.compact.Container;
import de.jpaw.bonaparte.pojos.very.compact.Vector;
import de.jpaw.bonaparte.scanner.BClassScanner;
import de.jpaw.util.ByteUtil;

public class TestComplex {
    @BeforeSuite
    public void registerClasses() {
        BClassScanner.init();
    }

    @Test
    public void testCompactComplexNumber() throws Exception {
    	System.out.println("Java version is " + System.getProperty("java.version"));
        Complex x = new Complex(42.0, 24.0);
        Complex yAndZ = new Complex(1, 0);

        Vector org = new Vector(x, yAndZ, yAndZ);

        CompactByteArrayComposer cbac = new CompactByteArrayComposer(1000, false);
        cbac.writeRecord(org);
        System.out.println("Length with CompactByteArrayComposer (ID) is " + cbac.getBuilder().length());

        // dump the bytes
        byte [] data = cbac.getBuilder().getBytes();
        System.out.println(ByteUtil.dump(data, 100));
        assert(data.length == 38);  // outer object per PQON, first two complex components as base object, last as repeated.

        // parse the result
        CompactByteArrayParser cbap = new CompactByteArrayParser(data, 0, data.length);
        BonaPortable copy = cbap.readRecord();
        assert(copy != null);
        assert(copy instanceof Vector);
        assert(org.equals(copy));
    }

    @Test
    public void testVeryCompactComplexNumber() throws Exception {
        // same test, but by calling the writeObject with an artificial outer reference, all types are known
        Complex x = new Complex(42.0, 24.0);
        Complex yAndZ = new Complex(1, 0);

        Vector org = new Vector(x, yAndZ, yAndZ);

        CompactByteArrayComposer cbac = new CompactByteArrayComposer(1000, false);
        cbac.addField(Container.meta$$outer, org);      // this line is different from the previous test case
        System.out.println("Length with CompactByteArrayComposer (ID) is " + cbac.getBuilder().length());

        // dump the bytes
        byte [] data = cbac.getBuilder().getBytes();
        System.out.println(ByteUtil.dump(data, 100));
        assert(data.length == 18);  // here we expect less than half the size of the previous test case!

        // parse the result
        CompactByteArrayParser cbap = new CompactByteArrayParser(data, 0, data.length);
        BonaPortable copy = cbap.readObject(Container.meta$$outer, Vector.class);
        assert(copy != null);
        assert(copy instanceof Vector);
        assert(org.equals(copy));
    }

    @Test
    public void testVeryCompactComplexNumberWithIds() throws Exception {
        // same test, but by calling the writeObject with an artificial outer reference, all types are known
        Complex x = new Complex(42.0, 24.0);
        Complex yAndZ = new Complex(1, 0);

        Vector org = new Vector(x, yAndZ, yAndZ);

        CompactByteArrayComposer cbac = new CompactByteArrayComposer(1000, true);
        cbac.addField(Container.meta$$outer, org);      // this line is different from the previous test case
        System.out.println("Length with CompactByteArrayComposer (ID) is " + cbac.getBuilder().length());

        // dump the bytes
        byte [] data = cbac.getBuilder().getBytes();
        System.out.println(ByteUtil.dump(data, 100));
        assert(data.length == 12);  // here we expect less than a third of the size of the initial test case!

        // parse the result
        CompactByteArrayParser cbap = new CompactByteArrayParser(data, 0, data.length);
        BonaPortable copy = cbap.readObject(Container.meta$$outer, Vector.class);
        assert(copy != null);
        assert(copy instanceof Vector);
        assert(org.equals(copy));
    }

}

@@ -1085,32 +1085,44 @@
 rayComposer (ID)
+ (expect 38)
  is %22 + cbac.get
@@ -1134,32 +1134,32 @@
 er().length());%0A
-
 %0A        // dump
@@ -1279,133 +1279,8 @@
 0));
-%0A        assert(data.length == 38);  // outer object per PQON, first two complex components as base object, last as repeated.
 %0A%0A  
@@ -1536,32 +1536,157 @@
 .equals(copy));%0A
+        assert(data.length == 38);  // outer object per PQON, first two complex components as base object, last as repeated.%0A
     %7D%0A%0A    @Test
@@ -2255,32 +2255,44 @@
 rayComposer (ID)
+ (expect 18)
  is %22 + cbac.get
@@ -2449,113 +2449,8 @@
 0));
-%0A        assert(data.length == 18);  // here we expect less than half the size of the previous test case!
 %0A%0A  
@@ -2745,24 +2745,129 @@
 als(copy));%0A
+        assert(data.length == 18);  // here we expect less than half the size of the previous test case!%0A
     %7D%0A%0A    @
@@ -3454,16 +3454,28 @@
 ser (ID)
+ (expect 12)
  is %22 + 
@@ -3640,118 +3640,8 @@
 0));
-%0A        assert(data.length == 12);  // here we expect less than a third of the size of the initial test case!
 %0A%0A  
@@ -3940,17 +3940,126 @@
 copy));%0A
+        assert(data.length == 12);  // here we expect less than a third of the size of the initial test case!%0A
     %7D%0A
-%0A
 %7D%0A

reorder checks