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