@@ -212,3 +212,102 @@ pub fn bytes_to_hex_lowercase(bytes_input: []const u8, hexadecimal_buffer: []u8)
212212 assert (hex_result .len == bytes_input .len * 2 );
213213 return hex_result ;
214214}
215+
216+ const testing = std .testing ;
217+
218+ test "TraceFlags fromU8 and toU8 conversion" {
219+ const flags_byte : u8 = 0b00000001 ;
220+ const flags = TraceFlags .fromU8 (flags_byte );
221+ try testing .expect (flags .sampled == true );
222+ try testing .expect (flags .toU8 () == flags_byte );
223+ }
224+
225+ test "TraceFlags sampled_only constructor" {
226+ const sampled_flags = TraceFlags .sampled_only (true );
227+ try testing .expect (sampled_flags .sampled == true );
228+ try testing .expect (sampled_flags .reserved_1 == false );
229+
230+ const unsampled_flags = TraceFlags .sampled_only (false );
231+ try testing .expect (unsampled_flags .sampled == false );
232+ }
233+
234+ test "TraceContext init creates valid context" {
235+ const ctx = TraceContext .init (true );
236+ try testing .expect (ctx .version == 0x00 );
237+ try testing .expect (ctx .trace_flags .sampled == true );
238+ try testing .expect (! is_all_zero_id (ctx .trace_id [0.. ]));
239+ try testing .expect (! is_all_zero_id (ctx .parent_id [0.. ]));
240+ try testing .expect (ctx .trace_id_hex .len == 32 );
241+ try testing .expect (ctx .span_id_hex .len == 16 );
242+ try testing .expect (ctx .parent_span_hex == null );
243+ }
244+
245+ test "TraceContext createChild maintains trace_id" {
246+ const parent_ctx = TraceContext .init (true );
247+ const child_ctx = parent_ctx .createChild (false );
248+
249+ try testing .expect (std .mem .eql (u8 , & child_ctx .trace_id , & parent_ctx .trace_id ));
250+ try testing .expect (! std .mem .eql (u8 , & child_ctx .parent_id , & parent_ctx .parent_id ));
251+ try testing .expect (child_ctx .trace_flags .sampled == false );
252+ try testing .expect (child_ctx .parent_span_hex != null );
253+ try testing .expect (! is_all_zero_id (child_ctx .parent_id [0.. ]));
254+ }
255+
256+ test "generate_trace_id produces valid IDs" {
257+ const trace_id1 = generate_trace_id ();
258+ const trace_id2 = generate_trace_id ();
259+
260+ try testing .expect (trace_id1 .len == 16 );
261+ try testing .expect (trace_id2 .len == 16 );
262+ try testing .expect (! is_all_zero_id (trace_id1 [0.. ]));
263+ try testing .expect (! is_all_zero_id (trace_id2 [0.. ]));
264+ try testing .expect (! std .mem .eql (u8 , & trace_id1 , & trace_id2 ));
265+ }
266+
267+ test "generate_span_id produces valid IDs" {
268+ const span_id1 = generate_span_id ();
269+ const span_id2 = generate_span_id ();
270+
271+ try testing .expect (span_id1 .len == 8 );
272+ try testing .expect (span_id2 .len == 8 );
273+ try testing .expect (! is_all_zero_id (span_id1 [0.. ]));
274+ try testing .expect (! is_all_zero_id (span_id2 [0.. ]));
275+ try testing .expect (! std .mem .eql (u8 , & span_id1 , & span_id2 ));
276+ }
277+
278+ test "expand_short_to_trace_id and extract_short_from_trace_id roundtrip" {
279+ const short_id : u64 = 0x123456789ABCDEF0 ;
280+ const expanded = expand_short_to_trace_id (short_id );
281+ const extracted = extract_short_from_trace_id (expanded );
282+
283+ try testing .expect (extracted == short_id );
284+ try testing .expect (expanded .len == 16 );
285+ try testing .expect (! is_all_zero_id (expanded [0.. ]));
286+ }
287+
288+ test "should_sample_from_trace_id with different rates" {
289+ const trace_id = generate_trace_id ();
290+
291+ try testing .expect (should_sample_from_trace_id (trace_id , 0 ) == false );
292+ try testing .expect (should_sample_from_trace_id (trace_id , 100 ) == true );
293+
294+ const sample_50 = should_sample_from_trace_id (trace_id , 50 );
295+ try testing .expect (@TypeOf (sample_50 ) == bool );
296+ }
297+
298+ test "is_all_zero_id detects zero arrays" {
299+ const zero_array = [_ ]u8 {0 } ** 16 ;
300+ const non_zero_array = [_ ]u8 { 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 };
301+
302+ try testing .expect (is_all_zero_id (zero_array [0.. ]));
303+ try testing .expect (! is_all_zero_id (non_zero_array [0.. ]));
304+ }
305+
306+ test "bytes_to_hex_lowercase conversion" {
307+ const bytes = [_ ]u8 { 0x00 , 0xFF , 0xAB , 0xCD };
308+ var hex_buffer : [8 ]u8 = undefined ;
309+
310+ const hex_result = try bytes_to_hex_lowercase (& bytes , & hex_buffer );
311+ try testing .expectEqualStrings ("00ffabcd" , hex_result );
312+ try testing .expect (hex_result .len == 8 );
313+ }
0 commit comments