|
| 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