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

     1  package types_test
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/Konstantin8105/c4go/program"
     9  	"github.com/Konstantin8105/c4go/types"
    10  )
    11  
    12  type resolveTestCase struct {
    13  	cType  string
    14  	goType string
    15  }
    16  
    17  var resolveTestCases = []resolveTestCase{
    18  	{"int", "int32"},
    19  	{"char *[13]", "[][]byte"},
    20  	{"void *", "interface{}"},
    21  	{"unsigned short int", "uint16"},
    22  	{"div_t", "noarch.DivT"},
    23  	{"ldiv_t", "noarch.LdivT"},
    24  	{"lldiv_t", "noarch.LldivT"},
    25  	{"int [2]", "[]int32"},
    26  	{"int [2][3]", "[][]int32"},
    27  	{"int [2][3][4]", "[][][]int32"},
    28  	{"int [2][3][4][5]", "[][][][]int32"},
    29  	{"int (*[2])(int, int)", "[2]func(int32,int32)(int32)"},
    30  	{"int (*(*(*)))(int, int)", "[][]func(int32,int32)(int32)"},
    31  }
    32  
    33  func TestResolve(t *testing.T) {
    34  	p := program.NewProgram()
    35  
    36  	for i, testCase := range resolveTestCases {
    37  		t.Run(fmt.Sprintf("Test %d : %s", i, testCase.cType), func(t *testing.T) {
    38  			goType, err := types.ResolveType(p, testCase.cType)
    39  			if err != nil {
    40  				t.Fatal(err)
    41  			}
    42  
    43  			goType = strings.Replace(goType, " ", "", -1)
    44  			testCase.goType = strings.Replace(testCase.goType, " ", "", -1)
    45  
    46  			if goType != testCase.goType {
    47  				t.Errorf("Expected '%s' -> '%s', got '%s'",
    48  					testCase.cType, testCase.goType, goType)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestResolveError(t *testing.T) {
    55  	tcs := []string{"w:w", "", "const"}
    56  	for i, tc := range tcs {
    57  		t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
    58  			var p program.Program
    59  			if _, err := types.ResolveType(&p, tc); err == nil {
    60  				t.Fatalf("Not acceptable")
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestGetAmountArraySize(t *testing.T) {
    67  	tcs := []struct {
    68  		cType string
    69  		value int
    70  		e     bool
    71  	}{
    72  		{
    73  			cType: "char [40]",
    74  			value: 40,
    75  			e:     false,
    76  		},
    77  		{
    78  			cType: "char",
    79  			e:     true,
    80  		},
    81  		{
    82  			cType: "unsigned char",
    83  			e:     true,
    84  		},
    85  	}
    86  
    87  	for _, tc := range tcs {
    88  		s, err := types.GetAmountArraySize(tc.cType, nil)
    89  		if err != nil && tc.e {
    90  			continue
    91  		}
    92  		if s != tc.value {
    93  			t.Errorf("%d != %d", s, tc.value)
    94  		}
    95  	}
    96  }