github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/types/sizeof_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Konstantin8105/c4go/program"
     7  	"github.com/Konstantin8105/c4go/types"
     8  )
     9  
    10  type sizeofTestCase struct {
    11  	cType   string
    12  	size    int
    13  	isError bool
    14  }
    15  
    16  var sizeofTestCases = []sizeofTestCase{
    17  	{"int", 4, true},
    18  	{"int [2]", 4 * 2, true},
    19  	{"int [2][3]", 4 * 2 * 3, true},
    20  	{"int [2][3][4]", 4 * 2 * 3 * 4, true},
    21  	{"int *[2]", 8 * 2, true},
    22  	{"int *[2][3]", 8 * 2 * 3, true},
    23  	{"int *[2][3][4]", 8 * 2 * 3 * 4, true},
    24  	{"int *", 8, true},
    25  	{"int **", 8, true},
    26  	{"int ***", 8, true},
    27  	{"char *const", 8, true},
    28  	{"char *const [3]", 24, true},
    29  	{"struct c [2]", 0, true},
    30  }
    31  
    32  func TestSizeOf(t *testing.T) {
    33  	p := program.NewProgram()
    34  
    35  	for _, testCase := range sizeofTestCases {
    36  		t.Run(testCase.cType, func(t *testing.T) {
    37  			size, err := types.SizeOf(p, testCase.cType)
    38  			if !((err != nil && testCase.isError == false) ||
    39  				(err == nil && testCase.isError == true)) {
    40  				t.Fatal(err)
    41  			}
    42  
    43  			if size != testCase.size {
    44  				t.Errorf("Expected '%s' -> '%d', got '%d'",
    45  					testCase.cType, testCase.size, size)
    46  			}
    47  		})
    48  	}
    49  }