2323
2424import org .junit .Test ;
2525
26- import static org . junit . Assert . assertFalse ;
27- import static org .junit .Assert .assertTrue ;
26+ import static at . favre . lib . bytes . BytesValidators .* ;
27+ import static org .junit .Assert .* ;
2828
2929public class BytesValidatorTest extends ABytesTest {
3030
@@ -36,28 +36,84 @@ public void testOnlyOfValidator() throws Exception {
3636 assertTrue (Bytes .wrap (new byte []{0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 }).validateNotOnlyZeros ());
3737 assertTrue (Bytes .random (128 ).validateNotOnlyZeros ());
3838
39- assertTrue (Bytes .allocate (1 ).validate (BytesValidators . onlyOf ((byte ) 0 )));
40- assertFalse (Bytes .allocate (1 ).validate (BytesValidators . noneOf ((byte ) 0 )));
41- assertFalse (Bytes .allocate (1 ).validate (BytesValidators . onlyOf ((byte ) 1 )));
42- assertTrue (Bytes .allocate (1 ).validate (BytesValidators . noneOf ((byte ) 1 )));
43- assertTrue (Bytes .allocate (1 ).validate (BytesValidators . noneOf ((byte ) 1 )));
44- assertTrue (Bytes .wrap (new byte []{1 , 1 , 1 , 1 , 0 , 1 }).validate (BytesValidators . notOnlyOf ((byte ) 1 )));
45- assertFalse (Bytes .wrap (new byte []{1 , 1 , 1 , 1 , 1 }).validate (BytesValidators . notOnlyOf ((byte ) 1 )));
46- assertTrue (Bytes .wrap (new byte []{1 , 1 , 1 , 1 , 1 , 1 }).validate (BytesValidators . onlyOf ((byte ) 1 )));
39+ assertTrue (Bytes .allocate (1 ).validate (onlyOf ((byte ) 0 )));
40+ assertFalse (Bytes .allocate (1 ).validate (noneOf ((byte ) 0 )));
41+ assertFalse (Bytes .allocate (1 ).validate (onlyOf ((byte ) 1 )));
42+ assertTrue (Bytes .allocate (1 ).validate (noneOf ((byte ) 1 )));
43+ assertTrue (Bytes .allocate (1 ).validate (noneOf ((byte ) 1 )));
44+ assertTrue (Bytes .wrap (new byte []{1 , 1 , 1 , 1 , 0 , 1 }).validate (notOnlyOf ((byte ) 1 )));
45+ assertFalse (Bytes .wrap (new byte []{1 , 1 , 1 , 1 , 1 }).validate (notOnlyOf ((byte ) 1 )));
46+ assertTrue (Bytes .wrap (new byte []{1 , 1 , 1 , 1 , 1 , 1 }).validate (onlyOf ((byte ) 1 )));
4747 }
4848
4949 @ Test
5050 public void testLengthValidators () throws Exception {
51- assertFalse (Bytes .allocate (0 ).validate (BytesValidators . atLeast (1 )));
52- assertTrue (Bytes .allocate (1 ).validate (BytesValidators . atLeast (1 )));
53- assertTrue (Bytes .allocate (2 ).validate (BytesValidators . atLeast (1 )));
51+ assertFalse (Bytes .allocate (0 ).validate (atLeast (1 )));
52+ assertTrue (Bytes .allocate (1 ).validate (atLeast (1 )));
53+ assertTrue (Bytes .allocate (2 ).validate (atLeast (1 )));
5454
55- assertFalse (Bytes .allocate (2 ).validate (BytesValidators . atMost (1 )));
56- assertTrue (Bytes .allocate (1 ).validate (BytesValidators . atMost (1 )));
57- assertTrue (Bytes .allocate (0 ).validate (BytesValidators . atMost (1 )));
55+ assertFalse (Bytes .allocate (2 ).validate (atMost (1 )));
56+ assertTrue (Bytes .allocate (1 ).validate (atMost (1 )));
57+ assertTrue (Bytes .allocate (0 ).validate (atMost (1 )));
5858
59- assertFalse (Bytes .allocate (0 ).validate (BytesValidators .exactLength (1 )));
60- assertTrue (Bytes .allocate (1 ).validate (BytesValidators .exactLength (1 )));
61- assertFalse (Bytes .allocate (2 ).validate (BytesValidators .exactLength (1 )));
59+ assertFalse (Bytes .allocate (0 ).validate (exactLength (1 )));
60+ assertTrue (Bytes .allocate (1 ).validate (exactLength (1 )));
61+ assertFalse (Bytes .allocate (2 ).validate (exactLength (1 )));
62+ }
63+
64+ @ Test
65+ public void testOrValidation () throws Exception {
66+ assertTrue (Bytes .allocate (0 ).validate (or (exactLength (1 ), exactLength (0 ))));
67+ assertTrue (Bytes .allocate (2 ).validate (or (atLeast (3 ), onlyOf ((byte ) 0 ))));
68+ assertTrue (Bytes .allocate (3 ).validate (or (onlyOf ((byte ) 1 ), onlyOf ((byte ) 0 ))));
69+ assertTrue (Bytes .wrap (new byte []{0 , 0 }).validate (or (onlyOf ((byte ) 1 ), onlyOf ((byte ) 0 ))));
70+ assertFalse (Bytes .wrap (new byte []{1 , 0 }).validate (or (onlyOf ((byte ) 2 ), onlyOf ((byte ) 1 ), onlyOf ((byte ) 0 ))));
71+ }
72+
73+ @ Test
74+ public void testAndValidation () throws Exception {
75+ assertFalse (Bytes .allocate (5 ).validate (and (atLeast (3 ), notOnlyOf ((byte ) 0 ))));
76+ assertFalse (Bytes .wrap (new byte []{1 , 0 }).validate (and (atLeast (3 ), notOnlyOf ((byte ) 0 ))));
77+ assertTrue (Bytes .wrap (new byte []{1 , 0 , 0 }).validate (and (atLeast (3 ), notOnlyOf ((byte ) 0 ))));
78+ assertFalse (Bytes .allocate (21 ).validate (and (atLeast (3 ), atMost (20 ))));
79+ }
80+
81+ @ Test
82+ public void testNotValidation () throws Exception {
83+ assertEquals (Bytes .allocate (2 ).validate (not (onlyOf ((byte ) 0 ))), Bytes .allocate (2 ).validate (notOnlyOf ((byte ) 0 )));
84+ assertTrue (Bytes .allocate (2 ).validate (not (atLeast (16 ))));
85+ assertFalse (Bytes .allocate (2 ).validate (not (atMost (16 ))));
86+ }
87+
88+ @ Test
89+ public void testNestedValidation () throws Exception {
90+ assertTrue (Bytes .allocate (16 ).validate (
91+ or (and (atLeast (8 ), not (onlyOf (((byte ) 0 )))),
92+ or (exactLength (16 ), exactLength (12 )))));
93+
94+ assertTrue (Bytes .allocate (16 ).validate (or (exactLength (16 ), exactLength (12 ))));
95+ assertFalse (Bytes .allocate (16 ).validate (and (atLeast (8 ), not (onlyOf (((byte ) 0 ))))));
96+ assertTrue (Bytes .allocate (16 ).validate (and (atLeast (8 ), onlyOf (((byte ) 0 )))));
97+ assertTrue (Bytes .allocate (16 ).validate (or (not (onlyOf (((byte ) 0 ))), exactLength (16 ))));
98+ }
99+
100+ @ Test
101+ public void testStartWithValidate () throws Exception {
102+ assertTrue (Bytes .wrap (new byte []{0 , 3 , 0 }).validate (startsWith ((byte ) 0 , (byte ) 3 )));
103+ assertFalse (Bytes .wrap (new byte []{0 , 2 , 0 }).validate (startsWith ((byte ) 0 , (byte ) 3 )));
104+ assertTrue (Bytes .wrap (new byte []{0 , 2 , 0 }).validate (startsWith ((byte ) 0 )));
105+ assertFalse (Bytes .wrap (new byte []{0 , 2 , 0 }).validate (startsWith ((byte ) 2 )));
106+ assertTrue (Bytes .allocate (16 ).validate (startsWith ((byte ) 0 )));
107+ assertFalse (Bytes .allocate (16 ).validate (startsWith (Bytes .allocate (17 ).array ())));
108+ }
109+
110+ @ Test
111+ public void testEndsWithValidate () throws Exception {
112+ assertTrue (Bytes .wrap (new byte []{1 , 2 , 3 }).validate (endsWith ((byte ) 2 , (byte ) 3 )));
113+ assertFalse (Bytes .wrap (new byte []{0 , 2 , 0 }).validate (endsWith ((byte ) 3 , (byte ) 0 )));
114+ assertTrue (Bytes .wrap (new byte []{0 , 2 , 0 }).validate (endsWith ((byte ) 0 )));
115+ assertFalse (Bytes .wrap (new byte []{0 , 2 , 0 }).validate (endsWith ((byte ) 2 )));
116+ assertTrue (Bytes .allocate (16 ).validate (endsWith ((byte ) 0 )));
117+ assertFalse (Bytes .allocate (16 ).validate (endsWith (Bytes .allocate (17 ).array ())));
62118 }
63119}
0 commit comments