github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/runtimecontext_test.go (about)

     1  package runtime
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestRuntimeResources_Dominates(t *testing.T) {
     9  	tests := []struct {
    10  		name string
    11  		r    RuntimeResources
    12  		v    RuntimeResources
    13  		want bool
    14  	}{
    15  		{
    16  			name: "both 0",
    17  			want: true,
    18  		},
    19  		{
    20  			name: "v > 0",
    21  			v: RuntimeResources{
    22  				Cpu:    1,
    23  				Memory: 2,
    24  				Millis: 3,
    25  			},
    26  			want: true,
    27  		},
    28  		{
    29  			name: "r > 0",
    30  			r: RuntimeResources{
    31  				Cpu:    1,
    32  				Memory: 2,
    33  				Millis: 3,
    34  			},
    35  			want: true,
    36  		},
    37  		{
    38  			name: "r > v > 0",
    39  			r: RuntimeResources{
    40  				Cpu:    10,
    41  				Memory: 20,
    42  				Millis: 30,
    43  			},
    44  			v: RuntimeResources{
    45  				Cpu:    5,
    46  				Memory: 10,
    47  				Millis: 10,
    48  			},
    49  			want: true,
    50  		},
    51  		{
    52  			name: "r.Cpu < v.Cpu",
    53  			r: RuntimeResources{
    54  				Cpu: 10,
    55  			},
    56  			v: RuntimeResources{
    57  				Cpu: 11,
    58  			},
    59  			want: false,
    60  		},
    61  		{
    62  			name: "r.Mem == v.Mem",
    63  			r: RuntimeResources{
    64  				Memory: 10,
    65  			},
    66  			v: RuntimeResources{
    67  				Memory: 10,
    68  			},
    69  			want: false,
    70  		},
    71  	}
    72  	for _, tt := range tests {
    73  		t.Run(tt.name, func(t *testing.T) {
    74  			if got := tt.r.Dominates(tt.v); got != tt.want {
    75  				t.Errorf("RuntimeResources.Dominates() = %v, want %v", got, tt.want)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  func TestRuntimeResources_Merge(t *testing.T) {
    82  	tests := []struct {
    83  		name string
    84  		r    RuntimeResources
    85  		r1   RuntimeResources
    86  		want RuntimeResources
    87  	}{
    88  		{
    89  			name: "all 0",
    90  		},
    91  		{
    92  			name: "Mix",
    93  			r: RuntimeResources{
    94  				Cpu:    10,
    95  				Memory: 20,
    96  				Millis: 30,
    97  			},
    98  			r1: RuntimeResources{
    99  				Cpu:    20,
   100  				Memory: 10,
   101  			},
   102  			want: RuntimeResources{
   103  				Cpu:    10,
   104  				Memory: 10,
   105  				Millis: 30,
   106  			},
   107  		},
   108  		{
   109  			name: "r == 0",
   110  			r1: RuntimeResources{
   111  				Cpu:    10,
   112  				Memory: 20,
   113  				Millis: 30,
   114  			},
   115  			want: RuntimeResources{
   116  				Cpu:    10,
   117  				Memory: 20,
   118  				Millis: 30,
   119  			},
   120  		},
   121  		{
   122  			name: "r1 > r",
   123  			r: RuntimeResources{
   124  				Cpu:    10,
   125  				Memory: 20,
   126  				Millis: 30,
   127  			},
   128  			r1: RuntimeResources{
   129  				Cpu:    100,
   130  				Memory: 200,
   131  				Millis: 300,
   132  			},
   133  			want: RuntimeResources{
   134  				Cpu:    10,
   135  				Memory: 20,
   136  				Millis: 30,
   137  			},
   138  		},
   139  	}
   140  	for _, tt := range tests {
   141  		t.Run(tt.name, func(t *testing.T) {
   142  
   143  			if got := tt.r.Merge(tt.r1); !reflect.DeepEqual(got, tt.want) {
   144  				t.Errorf("RuntimeResources.Merge() = %v, want %v", got, tt.want)
   145  			}
   146  			if got := tt.r1.Merge(tt.r); !reflect.DeepEqual(got, tt.want) {
   147  				t.Errorf("symmetric RuntimeResources.Merge() = %v, want %v", got, tt.want)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  func TestRuntimeResources_Remove(t *testing.T) {
   154  	tests := []struct {
   155  		name string
   156  		r    RuntimeResources
   157  		v    RuntimeResources
   158  		want RuntimeResources
   159  	}{
   160  		{
   161  			name: "all 0",
   162  		},
   163  		{
   164  			name: "r == 0",
   165  			v: RuntimeResources{
   166  				Cpu:    10,
   167  				Memory: 100,
   168  			},
   169  		},
   170  		{
   171  			name: "r > 0",
   172  			r: RuntimeResources{
   173  				Cpu:    100,
   174  				Memory: 10,
   175  				Millis: 50,
   176  			},
   177  			v: RuntimeResources{
   178  				Cpu:    50,
   179  				Millis: 100,
   180  			},
   181  			want: RuntimeResources{
   182  				Cpu:    50,
   183  				Memory: 10,
   184  			},
   185  		},
   186  	}
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			if got := tt.r.Remove(tt.v); !reflect.DeepEqual(got, tt.want) {
   190  				t.Errorf("RuntimeResources.Remove() = %v, want %v", got, tt.want)
   191  			}
   192  		})
   193  	}
   194  }