github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/array_test.go (about)

     1  package goja
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestArray1(t *testing.T) {
     9  	r := &Runtime{}
    10  	a := r.newArray(nil)
    11  	a.setOwnIdx(valueInt(0), asciiString("test"), true)
    12  	if l := a.getStr("length", nil).ToInteger(); l != 1 {
    13  		t.Fatalf("Unexpected length: %d", l)
    14  	}
    15  }
    16  
    17  func TestArrayExportProps(t *testing.T) {
    18  	vm := New()
    19  	arr := vm.NewArray()
    20  	err := arr.DefineDataProperty("0", vm.ToValue(true), FLAG_TRUE, FLAG_FALSE, FLAG_TRUE)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	actual := arr.Export()
    25  	expected := []interface{}{true}
    26  	if !reflect.DeepEqual(actual, expected) {
    27  		t.Fatalf("Expected: %#v, actual: %#v", expected, actual)
    28  	}
    29  }
    30  
    31  func TestArrayCanonicalIndex(t *testing.T) {
    32  	const SCRIPT = `
    33  	var a = [];
    34  	a["00"] = 1;
    35  	a["01"] = 2;
    36  	if (a[0] !== undefined) {
    37  		throw new Error("a[0]");
    38  	}
    39  	`
    40  
    41  	testScript(SCRIPT, _undefined, t)
    42  }
    43  
    44  func BenchmarkArrayGetStr(b *testing.B) {
    45  	b.StopTimer()
    46  	r := New()
    47  	v := &Object{runtime: r}
    48  
    49  	a := &arrayObject{
    50  		baseObject: baseObject{
    51  			val:        v,
    52  			extensible: true,
    53  		},
    54  	}
    55  	v.self = a
    56  
    57  	a.init()
    58  
    59  	v.setOwn(valueInt(0), asciiString("test"), false)
    60  	b.StartTimer()
    61  
    62  	for i := 0; i < b.N; i++ {
    63  		a.getStr("0", nil)
    64  	}
    65  
    66  }
    67  
    68  func BenchmarkArrayGet(b *testing.B) {
    69  	b.StopTimer()
    70  	r := New()
    71  	v := &Object{runtime: r}
    72  
    73  	a := &arrayObject{
    74  		baseObject: baseObject{
    75  			val:        v,
    76  			extensible: true,
    77  		},
    78  	}
    79  	v.self = a
    80  
    81  	a.init()
    82  
    83  	var idx Value = valueInt(0)
    84  
    85  	v.setOwn(idx, asciiString("test"), false)
    86  
    87  	b.StartTimer()
    88  
    89  	for i := 0; i < b.N; i++ {
    90  		v.get(idx, nil)
    91  	}
    92  
    93  }
    94  
    95  func BenchmarkArrayPut(b *testing.B) {
    96  	b.StopTimer()
    97  	r := New()
    98  
    99  	v := &Object{runtime: r}
   100  
   101  	a := &arrayObject{
   102  		baseObject: baseObject{
   103  			val:        v,
   104  			extensible: true,
   105  		},
   106  	}
   107  
   108  	v.self = a
   109  
   110  	a.init()
   111  
   112  	var idx Value = valueInt(0)
   113  	var val Value = asciiString("test")
   114  
   115  	b.StartTimer()
   116  
   117  	for i := 0; i < b.N; i++ {
   118  		v.setOwn(idx, val, false)
   119  	}
   120  
   121  }
   122  
   123  func BenchmarkArraySetEmpty(b *testing.B) {
   124  	r := New()
   125  	_ = r.Get("Array").(*Object).Get("prototype").String() // materialise Array.prototype
   126  	a := r.NewArray(0, 0)
   127  	values := a.self.(*arrayObject).values
   128  	b.ResetTimer()
   129  	for i := 0; i < b.N; i++ {
   130  		values[0] = nil
   131  		a.self.setOwnIdx(0, valueTrue, true)
   132  	}
   133  }