...

Source file src/github.com/tklauser/go-sysconf/cgotest/sysconf_cgotest.go

Documentation: github.com/tklauser/go-sysconf/cgotest

     1  // Copyright 2018 Tobias Klauser. 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  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     7  
     8  package sysconf_cgotest
     9  
    10  /*
    11  #include <unistd.h>
    12  */
    13  import "C"
    14  
    15  import (
    16  	"testing"
    17  
    18  	"github.com/tklauser/go-sysconf"
    19  )
    20  
    21  type testCase struct {
    22  	goVar int
    23  	cVar  C.int
    24  	name  string
    25  }
    26  
    27  func testSysconfGoCgo(t *testing.T, tc testCase) {
    28  	t.Helper()
    29  
    30  	if tc.goVar != int(tc.cVar) {
    31  		t.Errorf("SC_* parameter value for %v is %v, want %v", tc.name, tc.goVar, tc.cVar)
    32  	}
    33  
    34  	goVal, goErr := sysconf.Sysconf(tc.goVar)
    35  	if goErr != nil {
    36  		t.Errorf("Sysconf(%s/%d): %v", tc.name, tc.goVar, goErr)
    37  		return
    38  	}
    39  	t.Logf("%s = %v", tc.name, goVal)
    40  
    41  	cVal, cErr := C.sysconf(tc.cVar)
    42  	if cErr != nil {
    43  		t.Errorf("C.sysconf(%s/%d): %v", tc.name, tc.cVar, cErr)
    44  		return
    45  	}
    46  
    47  	if goVal != int64(cVal) {
    48  		t.Errorf("Sysconf(%v/%d) returned %v, want %v", tc.name, tc.goVar, goVal, cVal)
    49  	}
    50  }
    51  
    52  func testSysconfGoCgoInvalid(t *testing.T, tc testCase) {
    53  	t.Helper()
    54  
    55  	if tc.goVar != int(tc.cVar) {
    56  		t.Errorf("SC_* parameter value for %v is %v, want %v", tc.name, tc.goVar, tc.cVar)
    57  	}
    58  
    59  	_, goErr := sysconf.Sysconf(tc.goVar)
    60  	if goErr == nil {
    61  		t.Errorf("Sysconf(%s/%d) unexpectedly returned without error", tc.name, tc.goVar)
    62  		return
    63  	}
    64  
    65  	_, cErr := C.sysconf(tc.cVar)
    66  	if cErr == nil {
    67  		t.Errorf("C.sysconf(%s/%d) unexpectedly returned without error", tc.name, tc.goVar)
    68  	}
    69  }
    70  

View as plain text