2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger;
20 import net.ktnx.mobileledger.utils.Digest;
22 import org.junit.Test;
24 import static junit.framework.TestCase.assertEquals;
27 * Example local unit test, which will execute on the development machine (host).
29 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
31 public class DigestUnitTest {
33 public void digestToHexString_isCorrect() {
34 assertEquals('0', Digest.hexDigitFor(0));
35 assertEquals('1', Digest.hexDigitFor(1));
36 assertEquals('2', Digest.hexDigitFor(2));
37 assertEquals('3', Digest.hexDigitFor(3));
38 assertEquals('4', Digest.hexDigitFor(4));
39 assertEquals('5', Digest.hexDigitFor(5));
40 assertEquals('6', Digest.hexDigitFor(6));
41 assertEquals('7', Digest.hexDigitFor(7));
42 assertEquals('8', Digest.hexDigitFor(8));
43 assertEquals('9', Digest.hexDigitFor(9));
44 assertEquals('a', Digest.hexDigitFor(10));
45 assertEquals('b', Digest.hexDigitFor(11));
46 assertEquals('c', Digest.hexDigitFor(12));
47 assertEquals('d', Digest.hexDigitFor(13));
48 assertEquals('e', Digest.hexDigitFor(14));
49 assertEquals('f', Digest.hexDigitFor(15));
52 public void hexDigitsFor_isCorrect() {
53 assertEquals("00", String.valueOf(Digest.hexDigitsFor(0)));
54 assertEquals("10", String.valueOf(Digest.hexDigitsFor(16)));
55 assertEquals("ff", String.valueOf(Digest.hexDigitsFor(255)));
56 assertEquals("a0", String.valueOf(Digest.hexDigitsFor(160)));
57 assertEquals("10", String.valueOf(Digest.hexDigitsFor((byte) 16)));
58 assertEquals("ff", String.valueOf(Digest.hexDigitsFor((byte) -1)));
60 @Test(expected = ArithmeticException.class)
61 public void digestToHexString_throwsOnNegative() {
62 Digest.hexDigitFor(-1);
64 @Test(expected = ArithmeticException.class)
65 public void digestToHexString_throwsOnGreaterThan15() {
66 Digest.hexDigitFor(16);
68 @Test(expected = ArithmeticException.class)
69 public void hexDigitsFor_throwsOnNegative() {
70 final char[] chars = Digest.hexDigitsFor(-5);
72 @Test(expected = ArithmeticException.class)
73 public void hexDigitsFor_throwsOnGreatherThan255() {
74 final char[] chars = Digest.hexDigitsFor(256);