66use crate :: BufList ;
77use anyhow:: { Context , Result , bail, ensure} ;
88use bytes:: { Buf , Bytes } ;
9- use hegel:: generators;
9+ use hegel:: { DefaultGenerator , generators} ;
1010use std:: {
1111 fmt,
1212 io:: { self , BufRead , IoSliceMut , Read , Seek , SeekFrom } ,
@@ -27,7 +27,7 @@ fn hegel_cursor_ops(tc: hegel::TestCase) {
2727 eprintln ! ( "\n **** start! num_bytes={num_bytes}, num_ops={num_ops}" ) ;
2828
2929 for index in 0 ..num_ops {
30- let cursor_op = draw_cursor_op ( & tc , num_bytes) ;
30+ let cursor_op = tc . draw ( cursor_ops ( num_bytes) ) ;
3131 // apply_and_compare prints out the rest of the line.
3232 eprint ! ( "** index {}, operation {:?}: " , index, cursor_op) ;
3333 cursor_op
@@ -51,79 +51,98 @@ fn buf_lists(tc: hegel::TestCase) -> BufList {
5151 chunks. into_iter ( ) . map ( Bytes :: from) . collect ( )
5252}
5353
54- fn draw_cursor_op ( tc : & hegel:: TestCase , num_bytes : usize ) -> CursorOp {
54+ /// Unit enum for variant selection.
55+ ///
56+ /// The derived DefaultGenerator picks a variant uniformly at random, without
57+ /// generating any field data.
58+ #[ derive( Clone , Debug , DefaultGenerator ) ]
59+ enum CursorOpKind {
60+ SetPosition ,
61+ SeekStart ,
62+ SeekEnd ,
63+ SeekCurrent ,
64+ Read ,
65+ ReadVectored ,
66+ ReadExact ,
67+ Consume ,
68+ BufChunk ,
69+ BufAdvance ,
70+ BufChunksVectored ,
71+ BufCopyToBytes ,
72+ BufGetU8 ,
73+ BufGetU64 ,
74+ BufGetU64Le ,
5575 #[ cfg( feature = "tokio1" ) ]
56- let max_op: u8 = 15 ;
57- #[ cfg( not( feature = "tokio1" ) ) ]
58- let max_op: u8 = 14 ;
76+ PollRead ,
77+ }
5978
60- let op = tc. draw ( generators:: integers :: < u8 > ( ) . max_value ( max_op) ) ;
61- match op {
62- 0 => {
79+ #[ hegel:: composite]
80+ fn cursor_ops ( tc : hegel:: TestCase , num_bytes : usize ) -> CursorOp {
81+ match tc. draw ( generators:: default :: < CursorOpKind > ( ) ) {
82+ CursorOpKind :: SetPosition => {
6383 // Allow going past the end of the list a bit.
6484 let pos = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) as u64 ;
6585 CursorOp :: SetPosition ( pos)
6686 }
67- 1 => {
87+ CursorOpKind :: SeekStart => {
6888 // Allow going past the end of the list a bit.
6989 let pos = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) as u64 ;
7090 CursorOp :: SeekStart ( pos)
7191 }
72- 2 => {
92+ CursorOpKind :: SeekEnd => {
7393 // Allow going past the beginning and end of the list a bit.
7494 let raw = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 3 / 2 ) ) ;
7595 let offset = raw as i64 - ( 1 + num_bytes * 5 / 4 ) as i64 ;
7696 CursorOp :: SeekEnd ( offset)
7797 }
78- 3 => {
98+ CursorOpKind :: SeekCurrent => {
7999 let raw = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 3 / 2 ) ) ;
80100 // Center the index at roughly 0.
81101 let offset = raw as i64 - ( num_bytes * 3 / 4 ) as i64 ;
82102 CursorOp :: SeekCurrent ( offset)
83103 }
84- 4 => {
104+ CursorOpKind :: Read => {
85105 let buf_size = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) ;
86106 CursorOp :: Read ( buf_size)
87107 }
88- 5 => {
108+ CursorOpKind :: ReadVectored => {
89109 let n_bufs = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( 7 ) ) ;
90110 let sizes = ( 0 ..n_bufs)
91111 . map ( |_| tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes) ) )
92112 . collect ( ) ;
93113 CursorOp :: ReadVectored ( sizes)
94114 }
95- 6 => {
115+ CursorOpKind :: ReadExact => {
96116 let buf_size = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) ;
97117 CursorOp :: ReadExact ( buf_size)
98118 }
99- 7 => {
119+ CursorOpKind :: Consume => {
100120 let amt = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) ;
101121 CursorOp :: Consume ( amt)
102122 }
103- 8 => CursorOp :: BufChunk ,
104- 9 => {
123+ CursorOpKind :: BufChunk => CursorOp :: BufChunk ,
124+ CursorOpKind :: BufAdvance => {
105125 let amt = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) ;
106126 CursorOp :: BufAdvance ( amt)
107127 }
108- 10 => {
128+ CursorOpKind :: BufChunksVectored => {
109129 let num_iovs = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes) ) ;
110130 CursorOp :: BufChunksVectored ( num_iovs)
111131 }
112- 11 => {
132+ CursorOpKind :: BufCopyToBytes => {
113133 let len = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) ;
114134 CursorOp :: BufCopyToBytes ( len)
115135 }
116- 12 => CursorOp :: BufGetU8 ,
117- 13 => CursorOp :: BufGetU64 ,
118- 14 => CursorOp :: BufGetU64Le ,
136+ CursorOpKind :: BufGetU8 => CursorOp :: BufGetU8 ,
137+ CursorOpKind :: BufGetU64 => CursorOp :: BufGetU64 ,
138+ CursorOpKind :: BufGetU64Le => CursorOp :: BufGetU64Le ,
119139 #[ cfg( feature = "tokio1" ) ]
120- 15 => {
140+ CursorOpKind :: PollRead => {
121141 let capacity = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( num_bytes * 5 / 4 ) ) ;
122142 // filled is in 0..=capacity, to sometimes fill the whole buffer.
123143 let filled = tc. draw ( generators:: integers :: < usize > ( ) . max_value ( capacity) ) ;
124144 CursorOp :: PollRead { capacity, filled }
125145 }
126- _ => unreachable ! ( ) ,
127146 }
128147}
129148
0 commit comments