package main import "testing" // spec:26174-planctl-context-tokens/R6.3+D§3.3 // Synthetic stat line: benign comm, starttime=12345. func TestParseStatStartTime_simple(t *testing.T) { // 22 fields: pid (comm) state ppid pgrp session tty_nr tpgid flags minflt cminflt majflt cmajflt utime stime cutime cstime priority nice num_threads itrealvalue starttime line := "1234 (bash) S 1 1 1 0 -1 4194304 100 0 0 0 1 2 0 0 20 0 1 0 12345 ...\n" n, err := parseStatStartTime(line) if err != nil { t.Fatalf("parseStatStartTime: %v", err) } if n != 12345 { t.Errorf("got %d, want 12345", n) } } // spec:26174-planctl-context-tokens/R6.3+D§3.3 // comm containing spaces and parens: the "LAST ')'" parse strategy must still // find the field-22 starttime correctly. func TestParseStatStartTime_commWithParensAndSpaces(t *testing.T) { line := "1234 (my (weird) program) S 1 1 1 0 -1 4194304 100 0 0 0 1 2 0 0 20 0 1 0 99999 ...\n" n, err := parseStatStartTime(line) if err != nil { t.Fatalf("parseStatStartTime: %v", err) } if n != 99999 { t.Errorf("got %d, want 99999", n) } } // spec:26174-planctl-context-tokens/R6.3+D§3.3 func TestParseStatStartTime_noParenError(t *testing.T) { if _, err := parseStatStartTime("not a stat line"); err == nil { t.Fatal("expected error for missing ')'") } } // spec:26174-planctl-context-tokens/R6.3+D§3.3 func TestParseStatStartTime_tooFewFields(t *testing.T) { if _, err := parseStatStartTime("1 (x) S 1 1"); err == nil { t.Fatal("expected error for too few fields") } } // spec:26174-planctl-context-tokens/R6.3+D§3.3 func TestParseStatStartTime_nonNumeric(t *testing.T) { // 20 tail fields with a non-numeric in starttime slot. tail := "S 1 1 1 0 -1 4194304 100 0 0 0 1 2 0 0 20 0 1 0 NOPE" if _, err := parseStatStartTime("1 (x) " + tail); err == nil { t.Fatal("expected error for non-numeric starttime") } }