Skip to content

Commit 57aa904

Browse files
committed
feat(separate_trace_types_crate): Moved test test_simple_trace to codetracer_trace_writer
1 parent 6c1d7f1 commit 57aa904

File tree

2 files changed

+134
-133
lines changed

2 files changed

+134
-133
lines changed

codetracer_trace_writer/src/lib.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,137 @@ pub fn create_trace_writer(program: &str, args: &[String], format: TraceEventsFi
2626
TraceEventsFileFormat::Binary => Box::new(crate::cbor_zstd_writer::CborZstdTraceWriter::new(program, args)),
2727
}
2828
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use codetracer_trace_types::*;
33+
use std::path::Path;
34+
use crate::{non_streaming_trace_writer::NonStreamingTraceWriter, trace_writer::TraceWriter};
35+
36+
#[test]
37+
fn test_simple_trace() {
38+
let mut tracer = NonStreamingTraceWriter::new("path.small", &vec![]);
39+
let path = Path::new("/test/path.small");
40+
tracer.start(path, Line(1));
41+
tracer.register_step(path, Line(1));
42+
tracer.register_step(path, Line(2));
43+
tracer.register_asm(&["asm0".to_string(), "asm1".to_string()]);
44+
tracer.register_special_event(EventLogKind::Write, "test");
45+
tracer.register_special_event(EventLogKind::Write, "test2");
46+
tracer.register_special_event(EventLogKind::Error, "testError");
47+
48+
let function_path_id = tracer.ensure_path_id(&path);
49+
let function_line = Line(3);
50+
// -> function_id 1 after top level;
51+
let function_id = tracer.ensure_function_id("function", &path, function_line);
52+
assert!(function_id == FunctionId(1));
53+
54+
let before_temp_step = tracer.events.len();
55+
tracer.register_step(path, function_line);
56+
tracer.drop_last_step();
57+
// drop last step: drops steps[-1]/variables[-]
58+
assert_eq!(before_temp_step + 2, tracer.events.len());
59+
assert!(matches!(tracer.events.last().unwrap(), TraceLowLevelEvent::DropLastStep));
60+
61+
let args = vec![tracer.arg("a", NONE_VALUE), tracer.arg("b", NONE_VALUE)];
62+
tracer.register_call(function_id, args);
63+
// => arg-related variable/value events; auto call-step event; potentially variables; call event
64+
65+
assert!(tracer.events.len() > 3);
66+
// println!("{:#?}", tracer.events);
67+
// -4, -3 should be variables
68+
let should_be_step = &tracer.events[tracer.events.len() - 2];
69+
let should_be_call = &tracer.events[tracer.events.len() - 1];
70+
if let TraceLowLevelEvent::Step(StepRecord { path_id, line }) = should_be_step {
71+
assert_eq!(*path_id, function_path_id);
72+
assert_eq!(*line, function_line);
73+
} else {
74+
assert!(false, "expected a auto-registered step event before the last call one");
75+
}
76+
assert!(matches!(should_be_call, TraceLowLevelEvent::Call(CallRecord { .. })));
77+
78+
let int_value_1 = ValueRecord::Int {
79+
i: 1,
80+
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
81+
};
82+
let int_value_2 = ValueRecord::Int {
83+
i: 2,
84+
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
85+
};
86+
let int_value_3 = ValueRecord::Int {
87+
i: 3,
88+
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
89+
};
90+
91+
tracer.register_variable_with_full_value("test_variable", int_value_1.clone());
92+
93+
let not_supported_value = ValueRecord::Error {
94+
msg: "not supported".to_string(),
95+
type_id: NONE_TYPE_ID,
96+
};
97+
tracer.register_variable_with_full_value("test_variable2", not_supported_value);
98+
99+
tracer.register_cell_value(Place(0), int_value_1.clone());
100+
let type_id = tracer.ensure_type_id(TypeKind::Seq, "Vector<Int>");
101+
tracer.register_compound_value(
102+
Place(1),
103+
ValueRecord::Sequence {
104+
elements: vec![ValueRecord::Cell { place: Place(0) }], // #0
105+
is_slice: false,
106+
type_id,
107+
},
108+
);
109+
tracer.register_variable("test_variable3", Place(1));
110+
tracer.assign_cell(Place(1), int_value_2.clone());
111+
tracer.register_cell_value(Place(2), int_value_2.clone());
112+
tracer.assign_compound_item(Place(0), 0, Place(2));
113+
114+
tracer.register_return(NONE_VALUE);
115+
tracer.drop_variable("test_variable3");
116+
117+
// example of the history events
118+
tracer.bind_variable("variable1", Place(1));
119+
tracer.bind_variable("variable2", Place(2));
120+
tracer.bind_variable("variable3", Place(3));
121+
122+
tracer.register_variable_with_full_value("variable1", int_value_1.clone());
123+
tracer.register_variable_with_full_value("variable2", int_value_2.clone());
124+
tracer.register_variable_with_full_value("variable3", int_value_3.clone());
125+
126+
// tracer.assign_simple("variable1", "variable2", PassBy::Value);
127+
// tracer.assign_compound("variable1", &["variable2", "variable3"], PassBy::Value);
128+
129+
// more future-proof hopefully, if we add other kinds of RValue
130+
let rvalue_1 = tracer.simple_rvalue("variable2");
131+
tracer.assign("variable1", rvalue_1, PassBy::Value);
132+
let rvalue_2 = tracer.compound_rvalue(&["variable2".to_string(), "variable3".to_string()]);
133+
tracer.assign("variable1", rvalue_2, PassBy::Value);
134+
135+
// example for reference types
136+
let reference_type = TypeRecord {
137+
kind: TypeKind::Pointer,
138+
lang_type: "MyReference<Int>".to_string(),
139+
specific_info: TypeSpecificInfo::Pointer {
140+
dereference_type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
141+
},
142+
};
143+
let reference_type_id = tracer.ensure_raw_type_id(reference_type);
144+
let _reference_value = ValueRecord::Reference {
145+
dereferenced: Box::new(int_value_1.clone()),
146+
address: 0,
147+
mutable: false,
148+
type_id: reference_type_id,
149+
};
150+
151+
tracer.drop_variables(&["variable1".to_string(), "variable2".to_string(), "variable3".to_string()]);
152+
153+
assert_eq!(tracer.events.len(), 47);
154+
// visible with
155+
// cargo tets -- --nocapture
156+
// println!("{:#?}", tracer.events);
157+
158+
// tracer.store_trace_metadata(&PathBuf::from("trace_metadata.json")).unwrap();
159+
// tracer.store_trace_paths(&PathBuf::from("trace_paths.json")).unwrap();
160+
// tracer.store_trace_events(&PathBuf::from("trace.json")).unwrap();
161+
}
162+
}

runtime_tracing/src/lib.rs

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +0,0 @@
1-
#[cfg(test)]
2-
mod tests {
3-
use codetracer_trace_types::*;
4-
use std::path::Path;
5-
// use std::path::PathBuf;
6-
7-
#[test]
8-
fn test_simple_trace() {
9-
let mut tracer = NonStreamingTraceWriter::new("path.small", &vec![]);
10-
let path = Path::new("/test/path.small");
11-
tracer.start(path, Line(1));
12-
tracer.register_step(path, Line(1));
13-
tracer.register_step(path, Line(2));
14-
tracer.register_asm(&["asm0".to_string(), "asm1".to_string()]);
15-
tracer.register_special_event(EventLogKind::Write, "test");
16-
tracer.register_special_event(EventLogKind::Write, "test2");
17-
tracer.register_special_event(EventLogKind::Error, "testError");
18-
19-
let function_path_id = tracer.ensure_path_id(&path);
20-
let function_line = Line(3);
21-
// -> function_id 1 after top level;
22-
let function_id = tracer.ensure_function_id("function", &path, function_line);
23-
assert!(function_id == FunctionId(1));
24-
25-
let before_temp_step = tracer.events.len();
26-
tracer.register_step(path, function_line);
27-
tracer.drop_last_step();
28-
// drop last step: drops steps[-1]/variables[-]
29-
assert_eq!(before_temp_step + 2, tracer.events.len());
30-
assert!(matches!(tracer.events.last().unwrap(), TraceLowLevelEvent::DropLastStep));
31-
32-
let args = vec![tracer.arg("a", NONE_VALUE), tracer.arg("b", NONE_VALUE)];
33-
tracer.register_call(function_id, args);
34-
// => arg-related variable/value events; auto call-step event; potentially variables; call event
35-
36-
assert!(tracer.events.len() > 3);
37-
// println!("{:#?}", tracer.events);
38-
// -4, -3 should be variables
39-
let should_be_step = &tracer.events[tracer.events.len() - 2];
40-
let should_be_call = &tracer.events[tracer.events.len() - 1];
41-
if let TraceLowLevelEvent::Step(StepRecord { path_id, line }) = should_be_step {
42-
assert_eq!(*path_id, function_path_id);
43-
assert_eq!(*line, function_line);
44-
} else {
45-
assert!(false, "expected a auto-registered step event before the last call one");
46-
}
47-
assert!(matches!(should_be_call, TraceLowLevelEvent::Call(CallRecord { .. })));
48-
49-
let int_value_1 = ValueRecord::Int {
50-
i: 1,
51-
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
52-
};
53-
let int_value_2 = ValueRecord::Int {
54-
i: 2,
55-
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
56-
};
57-
let int_value_3 = ValueRecord::Int {
58-
i: 3,
59-
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
60-
};
61-
62-
tracer.register_variable_with_full_value("test_variable", int_value_1.clone());
63-
64-
let not_supported_value = ValueRecord::Error {
65-
msg: "not supported".to_string(),
66-
type_id: NONE_TYPE_ID,
67-
};
68-
tracer.register_variable_with_full_value("test_variable2", not_supported_value);
69-
70-
tracer.register_cell_value(Place(0), int_value_1.clone());
71-
let type_id = tracer.ensure_type_id(TypeKind::Seq, "Vector<Int>");
72-
tracer.register_compound_value(
73-
Place(1),
74-
ValueRecord::Sequence {
75-
elements: vec![ValueRecord::Cell { place: Place(0) }], // #0
76-
is_slice: false,
77-
type_id,
78-
},
79-
);
80-
tracer.register_variable("test_variable3", Place(1));
81-
tracer.assign_cell(Place(1), int_value_2.clone());
82-
tracer.register_cell_value(Place(2), int_value_2.clone());
83-
tracer.assign_compound_item(Place(0), 0, Place(2));
84-
85-
tracer.register_return(NONE_VALUE);
86-
tracer.drop_variable("test_variable3");
87-
88-
// example of the history events
89-
tracer.bind_variable("variable1", Place(1));
90-
tracer.bind_variable("variable2", Place(2));
91-
tracer.bind_variable("variable3", Place(3));
92-
93-
tracer.register_variable_with_full_value("variable1", int_value_1.clone());
94-
tracer.register_variable_with_full_value("variable2", int_value_2.clone());
95-
tracer.register_variable_with_full_value("variable3", int_value_3.clone());
96-
97-
// tracer.assign_simple("variable1", "variable2", PassBy::Value);
98-
// tracer.assign_compound("variable1", &["variable2", "variable3"], PassBy::Value);
99-
100-
// more future-proof hopefully, if we add other kinds of RValue
101-
let rvalue_1 = tracer.simple_rvalue("variable2");
102-
tracer.assign("variable1", rvalue_1, PassBy::Value);
103-
let rvalue_2 = tracer.compound_rvalue(&["variable2".to_string(), "variable3".to_string()]);
104-
tracer.assign("variable1", rvalue_2, PassBy::Value);
105-
106-
// example for reference types
107-
let reference_type = TypeRecord {
108-
kind: TypeKind::Pointer,
109-
lang_type: "MyReference<Int>".to_string(),
110-
specific_info: TypeSpecificInfo::Pointer {
111-
dereference_type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
112-
},
113-
};
114-
let reference_type_id = tracer.ensure_raw_type_id(reference_type);
115-
let _reference_value = ValueRecord::Reference {
116-
dereferenced: Box::new(int_value_1.clone()),
117-
address: 0,
118-
mutable: false,
119-
type_id: reference_type_id,
120-
};
121-
122-
tracer.drop_variables(&["variable1".to_string(), "variable2".to_string(), "variable3".to_string()]);
123-
124-
assert_eq!(tracer.events.len(), 47);
125-
// visible with
126-
// cargo tets -- --nocapture
127-
// println!("{:#?}", tracer.events);
128-
129-
// tracer.store_trace_metadata(&PathBuf::from("trace_metadata.json")).unwrap();
130-
// tracer.store_trace_paths(&PathBuf::from("trace_paths.json")).unwrap();
131-
// tracer.store_trace_events(&PathBuf::from("trace.json")).unwrap();
132-
}
133-
}

0 commit comments

Comments
 (0)