github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/funcid_test.go (about) 1 package lang 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/lmorg/murex/test/count" 8 ) 9 10 func TestFuncId(t *testing.T) { 11 f := newFuncID() 12 13 bad := uint32(1337) 14 err := f.Executing(bad) 15 if err == nil { 16 t.Errorf("fid %d: err should NOT be nil", bad) 17 } 18 19 _, err = f.Proc(bad) 20 if err == nil { 21 t.Errorf("fid %d: err should NOT be nil", bad) 22 } 23 24 bad = 0 25 _, err = f.Proc(0) 26 if err == nil { 27 t.Errorf("fid %d: err should NOT be nil", bad) 28 } 29 30 count.Tests(t, 3) 31 32 var tests [5]*Process 33 34 count.Tests(t, len(tests)*6) 35 36 for i := range tests { 37 tests[i] = NewTestProcess() 38 tests[i].Name.Set(strconv.Itoa(i)) 39 fid := f.Register(tests[i]) 40 41 if fid != tests[i].Id { 42 t.Errorf("test %d: fid %d: fid != p.Id (%d)", i, fid, tests[i].Id) 43 } 44 45 _, err = f.Proc(fid) 46 if err == nil { 47 t.Errorf("test %d: fid %d: err should NOT be nil", i, fid) 48 } 49 50 err := f.Executing(fid) 51 if err != nil { 52 t.Errorf("test %d: fid %d: err should be nil: %s", i, fid, err.Error()) 53 } 54 55 p, err := f.Proc(fid) 56 if err != nil { 57 t.Errorf("test %d: fid %d: unexpected error: %s", i, fid, err.Error()) 58 } 59 60 if p.Id != fid { 61 t.Errorf("test %d: fid %d: fid != p.Id (%d)", i, fid, p.Id) 62 } 63 64 f.Deregister(fid) 65 err = f.Executing(fid) 66 if err == nil { 67 t.Errorf("test %d: fid %d: err should NOT be nil", i, fid) 68 } 69 } 70 } 71 72 func TestFuncIdListAllEmpty(t *testing.T) { 73 f := newFuncID() 74 75 count.Tests(t, 1) 76 77 c := 100 78 79 for i := 0; i < c; i++ { 80 p := NewTestProcess() 81 f.Register(p) 82 } 83 84 list := f.ListAll() 85 if len(list) != 0 { 86 t.Error("list includes non-executed processes") 87 } 88 } 89 90 func TestFuncIdListAllFull(t *testing.T) { 91 f := newFuncID() 92 93 count.Tests(t, 2) 94 95 c := 100 96 97 for i := 0; i < c; i++ { 98 p := NewTestProcess() 99 fid := f.Register(p) 100 f.Executing(fid) 101 } 102 103 list := f.ListAll() 104 if len(list) != c { 105 t.Error("list != c") 106 } 107 108 for i := 0; i < c-1; i++ { 109 if list[i].Id >= list[i+1].Id { 110 t.Errorf("list not sorted") 111 } 112 } 113 }