Skip to content

Instantly share code, notes, and snippets.

@DHager
Created April 1, 2011 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DHager/897683 to your computer and use it in GitHub Desktop.
Save DHager/897683 to your computer and use it in GitHub Desktop.
import org.codehaus.preon.Codec;
import org.codehaus.preon.Codecs;
import org.codehaus.preon.annotation.Bound;
import org.codehaus.preon.annotation.BoundNumber;
import org.codehaus.preon.annotation.If;
import org.junit.Assert;
import org.junit.Test;
public class Preon46 {
public static class Preon46Problem {
@Bound
boolean someflag;
@Bound
boolean realflag;
@If("realflag")
@BoundNumber(size = "6")
int num;
}
public static class NewlyFoundProblem {
@Bound
boolean someflag;
@Bound
boolean realflag;
@If("realflag==1")
@BoundNumber(size = "6")
int num;
}
private static byte binaryConvert(String s) {
return (byte) Integer.parseInt(s, 2);
}
/*
* Tests a reported issue where the @If flag was not properly working.
*/
@Test
public void testPreon46() throws Exception {
byte[] data1 = new byte[]{binaryConvert("01010011"), 19};
byte[] data2 = new byte[]{binaryConvert("00010011"), 0};
Codec<Preon46Problem> testClassCodec = Codecs.create(Preon46Problem.class);
Preon46Problem decode;
decode = Codecs.decode(testClassCodec, data1);
Assert.assertEquals(19, decode.num); // Fails here
decode = Codecs.decode(testClassCodec, data2);
Assert.assertEquals(0, decode.num);
}
/*
* Tests a new issue found during experimentation
*/
@Test
public void testNewProblem() throws Exception {
byte[] data1 = new byte[]{binaryConvert("01010011"), 19};
byte[] data2 = new byte[]{binaryConvert("00010011"), 0};
Codec<NewlyFoundProblem> testClassCodec = Codecs.create(NewlyFoundProblem.class);
NewlyFoundProblem decode;
decode = Codecs.decode(testClassCodec, data1);
Assert.assertEquals(19, decode.num); // Fails here, but from NullPointerException in describe step
decode = Codecs.decode(testClassCodec, data2);
Assert.assertEquals(0, decode.num);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment