Source file src/runtime/list_test.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"runtime"
     9  	"testing"
    10  	"unsafe"
    11  )
    12  
    13  type listedVal struct {
    14  	val int
    15  
    16  	aNode runtime.ListNode
    17  	bNode runtime.ListNode
    18  }
    19  
    20  func newListedVal(v int) *listedVal {
    21  	return &listedVal{
    22  		val: v,
    23  	}
    24  }
    25  
    26  func TestListPush(t *testing.T) {
    27  	var headA runtime.ListHead
    28  	headA.Init(unsafe.Offsetof(listedVal{}.aNode))
    29  
    30  	one := newListedVal(1)
    31  	headA.Push(unsafe.Pointer(one))
    32  
    33  	two := newListedVal(2)
    34  	headA.Push(unsafe.Pointer(two))
    35  
    36  	three := newListedVal(3)
    37  	headA.Push(unsafe.Pointer(three))
    38  
    39  	p := headA.Pop()
    40  	v := (*listedVal)(p)
    41  	if v == nil {
    42  		t.Fatalf("pop got nil want 3")
    43  	}
    44  	if v.val != 3 {
    45  		t.Errorf("pop got %d want 3", v.val)
    46  	}
    47  
    48  	p = headA.Pop()
    49  	v = (*listedVal)(p)
    50  	if v == nil {
    51  		t.Fatalf("pop got nil want 2")
    52  	}
    53  	if v.val != 2 {
    54  		t.Errorf("pop got %d want 2", v.val)
    55  	}
    56  
    57  	p = headA.Pop()
    58  	v = (*listedVal)(p)
    59  	if v == nil {
    60  		t.Fatalf("pop got nil want 1")
    61  	}
    62  	if v.val != 1 {
    63  		t.Errorf("pop got %d want 1", v.val)
    64  	}
    65  
    66  	p = headA.Pop()
    67  	v = (*listedVal)(p)
    68  	if v != nil {
    69  		t.Fatalf("pop got %+v want nil", v)
    70  	}
    71  }
    72  
    73  func wantVal(t *testing.T, v *listedVal, i int) {
    74  	t.Helper()
    75  	if v == nil {
    76  		t.Fatalf("listedVal got nil want %d", i)
    77  	}
    78  	if v.val != i {
    79  		t.Errorf("pop got %d want %d", v.val, i)
    80  	}
    81  }
    82  
    83  func TestListRemoveHead(t *testing.T) {
    84  	var headA runtime.ListHead
    85  	headA.Init(unsafe.Offsetof(listedVal{}.aNode))
    86  
    87  	one := newListedVal(1)
    88  	headA.Push(unsafe.Pointer(one))
    89  
    90  	two := newListedVal(2)
    91  	headA.Push(unsafe.Pointer(two))
    92  
    93  	three := newListedVal(3)
    94  	headA.Push(unsafe.Pointer(three))
    95  
    96  	headA.Remove(unsafe.Pointer(three))
    97  
    98  	p := headA.Pop()
    99  	v := (*listedVal)(p)
   100  	wantVal(t, v, 2)
   101  
   102  	p = headA.Pop()
   103  	v = (*listedVal)(p)
   104  	wantVal(t, v, 1)
   105  
   106  	p = headA.Pop()
   107  	v = (*listedVal)(p)
   108  	if v != nil {
   109  		t.Fatalf("pop got %+v want nil", v)
   110  	}
   111  }
   112  
   113  func TestListRemoveMiddle(t *testing.T) {
   114  	var headA runtime.ListHead
   115  	headA.Init(unsafe.Offsetof(listedVal{}.aNode))
   116  
   117  	one := newListedVal(1)
   118  	headA.Push(unsafe.Pointer(one))
   119  
   120  	two := newListedVal(2)
   121  	headA.Push(unsafe.Pointer(two))
   122  
   123  	three := newListedVal(3)
   124  	headA.Push(unsafe.Pointer(three))
   125  
   126  	headA.Remove(unsafe.Pointer(two))
   127  
   128  	p := headA.Pop()
   129  	v := (*listedVal)(p)
   130  	wantVal(t, v, 3)
   131  
   132  	p = headA.Pop()
   133  	v = (*listedVal)(p)
   134  	wantVal(t, v, 1)
   135  
   136  	p = headA.Pop()
   137  	v = (*listedVal)(p)
   138  	if v != nil {
   139  		t.Fatalf("pop got %+v want nil", v)
   140  	}
   141  }
   142  
   143  func TestListRemoveTail(t *testing.T) {
   144  	var headA runtime.ListHead
   145  	headA.Init(unsafe.Offsetof(listedVal{}.aNode))
   146  
   147  	one := newListedVal(1)
   148  	headA.Push(unsafe.Pointer(one))
   149  
   150  	two := newListedVal(2)
   151  	headA.Push(unsafe.Pointer(two))
   152  
   153  	three := newListedVal(3)
   154  	headA.Push(unsafe.Pointer(three))
   155  
   156  	headA.Remove(unsafe.Pointer(one))
   157  
   158  	p := headA.Pop()
   159  	v := (*listedVal)(p)
   160  	wantVal(t, v, 3)
   161  
   162  	p = headA.Pop()
   163  	v = (*listedVal)(p)
   164  	wantVal(t, v, 2)
   165  
   166  	p = headA.Pop()
   167  	v = (*listedVal)(p)
   168  	if v != nil {
   169  		t.Fatalf("pop got %+v want nil", v)
   170  	}
   171  }
   172  
   173  func TestListRemoveAll(t *testing.T) {
   174  	var headA runtime.ListHead
   175  	headA.Init(unsafe.Offsetof(listedVal{}.aNode))
   176  
   177  	one := newListedVal(1)
   178  	headA.Push(unsafe.Pointer(one))
   179  
   180  	two := newListedVal(2)
   181  	headA.Push(unsafe.Pointer(two))
   182  
   183  	three := newListedVal(3)
   184  	headA.Push(unsafe.Pointer(three))
   185  
   186  	headA.Remove(unsafe.Pointer(one))
   187  	headA.Remove(unsafe.Pointer(two))
   188  	headA.Remove(unsafe.Pointer(three))
   189  
   190  	p := headA.Pop()
   191  	v := (*listedVal)(p)
   192  	if v != nil {
   193  		t.Fatalf("pop got %+v want nil", v)
   194  	}
   195  }
   196  
   197  func BenchmarkListPushPop(b *testing.B) {
   198  	var head runtime.ListHead
   199  	head.Init(unsafe.Offsetof(listedVal{}.aNode))
   200  
   201  	vals := make([]listedVal, 10000)
   202  	i := 0
   203  	for b.Loop() {
   204  		if i == len(vals) {
   205  			for range len(vals) {
   206  				head.Pop()
   207  			}
   208  			i = 0
   209  		}
   210  
   211  		head.Push(unsafe.Pointer(&vals[i]))
   212  
   213  		i++
   214  	}
   215  }
   216  

View as plain text