Skip to content

Commit 75d973d

Browse files
committed
added some extra comment for JBBPTextWriter#Bin(), added one more test
1 parent 6e912e7 commit 75d973d

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/main/java/com/igormaznitsa/jbbp/utils/JBBPTextWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,8 +1507,9 @@ public JBBPTextWriter Obj(final int objId, final Object[] array, int off, int le
15071507
}
15081508

15091509
/**
1510-
* Print objects which marked by Bin annotation or successors of
1511-
* JBBPAbstractField.
1510+
* Print objects which marked by Bin annotation or successors of JBBPAbstractField.
1511+
* <b>NB! Keep in mind that values of fields will be processed for their attributes before printing
1512+
* and for instance a bit field with inversion will be shown as inverted one.</b>
15121513
*
15131514
* @param objs array of object marked by Bin annotation or successors of
15141515
* JBBPAbstractField
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2015 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.igormaznitsa.jbbp.it;
17+
18+
import com.igormaznitsa.jbbp.JBBPParser;
19+
import com.igormaznitsa.jbbp.io.JBBPBitNumber;
20+
import com.igormaznitsa.jbbp.io.JBBPBitOrder;
21+
import com.igormaznitsa.jbbp.io.JBBPOut;
22+
import com.igormaznitsa.jbbp.mapper.Bin;
23+
import com.igormaznitsa.jbbp.mapper.BinType;
24+
import org.junit.Test;
25+
import static org.junit.Assert.*;
26+
27+
/**
28+
* Tests based on questions and cases.
29+
*/
30+
public class BasedOnQuestionsAndCasesTest {
31+
32+
/**
33+
* Case 13-aug-2015
34+
*
35+
* 3DF8 = 0011 1101 1111 1000 where data are stored from left to right :
36+
* 6 bits : 0011 11 for year;
37+
* 4 bits : 01 11 for month
38+
* 5 bits : 11 100 for day,
39+
*
40+
* @throws Exception for any error
41+
*/
42+
@Test
43+
public void testParseDayMonthYearFromBytePairInMSB0AndPackThemBack() throws Exception {
44+
class YearMonthDay {
45+
@Bin(type = BinType.BIT, outBitNumber = JBBPBitNumber.BITS_6, outOrder = 1, bitOrder = JBBPBitOrder.MSB0) byte year;
46+
@Bin(type = BinType.BIT, outBitNumber = JBBPBitNumber.BITS_4, outOrder = 2, bitOrder = JBBPBitOrder.MSB0) byte month;
47+
@Bin(type = BinType.BIT, outBitNumber = JBBPBitNumber.BITS_5, outOrder = 3, bitOrder = JBBPBitOrder.MSB0) byte day;
48+
}
49+
final YearMonthDay parsed = JBBPParser.prepare("bit:6 year; bit:4 month; bit:5 day;", JBBPBitOrder.MSB0).parse(new byte[]{(byte) 0x3D, (byte) 0xF8}).mapTo(YearMonthDay.class);
50+
51+
assertEquals(0x0F,parsed.year);
52+
assertEquals(0x07,parsed.month);
53+
assertEquals(0x1C,parsed.day & 0xFF);
54+
55+
assertArrayEquals(new byte[]{(byte)0x3D,(byte)0xF8}, JBBPOut.BeginBin(JBBPBitOrder.MSB0).Bin(parsed).End().toByteArray());
56+
}
57+
}

0 commit comments

Comments
 (0)