109 lines
5.0 KiB
Ziran
109 lines
5.0 KiB
Ziran
// Independent bounded oracle for stable row/group ordering and hash grouping.
|
|
Model :: #import "model";
|
|
source: Model.Snapshot;
|
|
groups: Model.GroupedSnapshot;
|
|
expected_groups: Model.GroupedSnapshot;
|
|
actual: [64]s32;
|
|
expected: [64]s32;
|
|
|
|
Compare :: (a: *Model.Process, b: *Model.Process, column: s32) -> s32 {
|
|
if column == Model.COLUMN_NAME {
|
|
for index: cast(s32)0..31 {
|
|
if index >= a.name_count || index >= b.name_count { break }
|
|
x := a.name[index]; y := b.name[index]
|
|
if x < y { return -1 }; if x > y { return 1 }
|
|
}
|
|
if a.name_count < b.name_count { return -1 }
|
|
if a.name_count > b.name_count { return 1 }
|
|
return 0
|
|
}
|
|
if column == Model.COLUMN_CPU {
|
|
if a.cpu_percent < b.cpu_percent { return -1 }; if a.cpu_percent > b.cpu_percent { return 1 }
|
|
return 0
|
|
}
|
|
x: u64; y: u64
|
|
if column == Model.COLUMN_PID { x = cast(u64)a.pid; y = cast(u64)b.pid }
|
|
if column == Model.COLUMN_CPU_TIME { x = a.ticks; y = b.ticks }
|
|
if column == Model.COLUMN_MEMORY { x = a.memory_bytes; y = b.memory_bytes }
|
|
if x < y { return -1 }; if x > y { return 1 }; return 0
|
|
}
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
source.count = 64
|
|
for index: cast(s32)0..63 {
|
|
entry := *source.processes[index]
|
|
entry.pid = index + 1
|
|
entry.name_count = 2
|
|
entry.name[0] = cast(u8)(65 + index % 3)
|
|
if index % 2 == 0 { entry.name[0] += cast(u8)32 }
|
|
entry.name[1] = cast(u8)(48 + index % 5)
|
|
entry.cpu_percent = cast(float64)(index % 4)
|
|
entry.memory_bytes = cast(u64)((index * 17) % 11)
|
|
entry.ticks = cast(u64)((index * 7) % 9)
|
|
}
|
|
// Different Codex aliases must occupy the same first-seen group.
|
|
for index: cast(s32)0..1 {
|
|
entry := *source.processes[index]
|
|
entry.name_count = 6
|
|
entry.name[0] = 67; entry.name[1] = 111; entry.name[2] = 100
|
|
entry.name[3] = 101; entry.name[4] = 120; entry.name[5] = cast(u8)(65 + index)
|
|
}
|
|
Model.BuildGroupedSnapshot(*source, *groups)
|
|
for index: cast(s32)0..63 {
|
|
entry := *source.processes[index]
|
|
group: s32 = -1
|
|
for candidate: 0..expected_groups.count - 1 {
|
|
if Model.SameProcessGroup(*source.processes[expected_groups.groups[candidate].process_index], entry) {
|
|
group = candidate; break
|
|
}
|
|
}
|
|
if group < 0 { group = expected_groups.count; expected_groups.count += 1; expected_groups.groups[group].process_index = index }
|
|
expected_groups.groups[group].group_count += 1
|
|
expected_groups.groups[group].cpu_percent += entry.cpu_percent
|
|
expected_groups.groups[group].memory_bytes += entry.memory_bytes
|
|
expected_groups.groups[group].ticks += entry.ticks
|
|
}
|
|
if groups.count != expected_groups.count { return 1 }
|
|
for index: cast(s32)0..groups.count - 1 {
|
|
a := groups.groups[index]; b := expected_groups.groups[index]
|
|
if a.process_index != b.process_index || a.group_count != b.group_count ||
|
|
a.cpu_percent != b.cpu_percent || a.memory_bytes != b.memory_bytes || a.ticks != b.ticks { return 2 }
|
|
}
|
|
for mode: cast(s32)0..1 {
|
|
count := ifx mode == 0 then source.count else groups.count
|
|
for column: cast(s32)0..Model.COLUMNS - 1 {
|
|
for direction: cast(s32)-1..1 {
|
|
for index: cast(s32)0..count - 1 { expected[index] = index }
|
|
if direction != 0 {
|
|
for slot: 1..count - 1 {
|
|
key := expected[slot]
|
|
key_row := source.processes[key]
|
|
if mode == 1 { key_row = Model.GroupProcess(*source, *groups.groups[key]) }
|
|
at := slot - 1
|
|
while at >= 0 {
|
|
row := source.processes[expected[at]]
|
|
if mode == 1 { row = Model.GroupProcess(*source, *groups.groups[expected[at]]) }
|
|
if Compare(*row, *key_row, column) * direction <= 0 { break }
|
|
expected[at + 1] = expected[at]; at -= 1
|
|
}
|
|
expected[at + 1] = key
|
|
}
|
|
}
|
|
if mode == 0 { Model.SortOrder(*source, actual[0:count], column, direction) }
|
|
else { Model.SortGroupOrder(*source, *groups, actual[0:count], column, direction) }
|
|
for index: cast(s32)0..count - 1 { if actual[index] != expected[index] {
|
|
print("ordering mismatch: groups=% column=% direction=% at=% actual=% expected=%\n", mode, column, direction, index, actual[index], expected[index])
|
|
return 3
|
|
} }
|
|
}
|
|
}
|
|
}
|
|
// Odd sizes and truncated output must retain stability too.
|
|
source.count = 31
|
|
Model.SortOrder(*source, actual[0:17], Model.COLUMN_PID, -1)
|
|
for index: cast(s32)0..16 { if actual[index] != 16 - index { return 4 } }
|
|
print("taskmgr ordering oracle passed\n")
|
|
return 0
|
|
}
|