github.com/llir/llvm@v0.3.6/ir/types/types_test.go (about) 1 package types 2 3 import "testing" 4 5 func TestIntTypeEqual(t *testing.T) { 6 golden := []struct { 7 t *IntType 8 u *IntType 9 want bool 10 }{ 11 { 12 t: &IntType{BitSize: 32}, 13 u: &IntType{BitSize: 8}, 14 want: false, 15 }, 16 { 17 t: &IntType{BitSize: 32}, 18 u: I32, 19 want: true, 20 }, 21 { 22 t: &IntType{BitSize: 32}, 23 u: NewInt(32), 24 want: true, 25 }, 26 { 27 t: &IntType{TypeName: "foo", BitSize: 32}, 28 u: &IntType{TypeName: "foo", BitSize: 8}, 29 want: false, 30 }, 31 { 32 t: &IntType{TypeName: "foo", BitSize: 32}, 33 u: I32, 34 want: true, 35 }, 36 { 37 t: &IntType{TypeName: "foo", BitSize: 32}, 38 u: NewInt(32), 39 want: true, 40 }, 41 } 42 for _, g := range golden { 43 got := g.t.Equal(g.u) 44 if g.want != got { 45 t.Errorf("struct equality mismatch between `%s` and `%s`; expected %t, got %t", g.t.LLString(), g.u.LLString(), g.want, got) 46 } 47 } 48 } 49 50 func TestIsVoid(t *testing.T) { 51 golden := []struct { 52 t Type 53 want bool 54 }{ 55 {&VoidType{}, true}, 56 {Void, true}, 57 {I8, false}, 58 } 59 for _, g := range golden { 60 got := IsVoid(g.t) 61 if g.want != got { 62 t.Errorf("check if `%s` is a void type mismatch; expected %t, got %t", g.t, g.want, got) 63 } 64 } 65 } 66 67 func TestIsFunc(t *testing.T) { 68 golden := []struct { 69 t Type 70 want bool 71 }{ 72 {&FuncType{RetType: Void}, true}, 73 {NewFunc(Void), true}, 74 {NewFunc(I8, I8), true}, 75 {I8, false}, 76 } 77 for _, g := range golden { 78 got := IsFunc(g.t) 79 if g.want != got { 80 t.Errorf("check if `%s` is a function type mismatch; expected %t, got %t", g.t, g.want, got) 81 } 82 } 83 } 84 85 func TestIsInt(t *testing.T) { 86 golden := []struct { 87 t Type 88 want bool 89 }{ 90 {t: &IntType{BitSize: 123}, want: true}, 91 {t: NewInt(123), want: true}, 92 {t: I1, want: true}, 93 {t: I8, want: true}, 94 {t: I16, want: true}, 95 {t: I32, want: true}, 96 {t: I64, want: true}, 97 {t: I128, want: true}, 98 {t: Void, want: false}, 99 {t: Double, want: false}, 100 } 101 for _, g := range golden { 102 got := IsInt(g.t) 103 if g.want != got { 104 t.Errorf("check if `%s` is an integer type mismatch; expected %t, got %t", g.t, g.want, got) 105 } 106 } 107 } 108 109 func TestIsFloat(t *testing.T) { 110 golden := []struct { 111 t Type 112 want bool 113 }{ 114 {t: &FloatType{Kind: FloatKindDouble}, want: true}, 115 {t: Half, want: true}, 116 {t: Float, want: true}, 117 {t: Double, want: true}, 118 {t: FP128, want: true}, 119 {t: X86_FP80, want: true}, 120 {t: PPC_FP128, want: true}, 121 {t: I8, want: false}, 122 } 123 for _, g := range golden { 124 got := IsFloat(g.t) 125 if g.want != got { 126 t.Errorf("check if `%s` is a floating-point type mismatch; expected %t, got %t", g.t, g.want, got) 127 } 128 } 129 } 130 131 func TestIsMMX(t *testing.T) { 132 golden := []struct { 133 t Type 134 want bool 135 }{ 136 {t: &MMXType{}, want: true}, 137 {t: MMX, want: true}, 138 {t: I8, want: false}, 139 } 140 for _, g := range golden { 141 got := IsMMX(g.t) 142 if g.want != got { 143 t.Errorf("check if `%s` is an MMX type mismatch; expected %t, got %t", g.t, g.want, got) 144 } 145 } 146 } 147 148 func TestIsPointer(t *testing.T) { 149 golden := []struct { 150 t Type 151 want bool 152 }{ 153 {t: &PointerType{ElemType: I8}, want: true}, 154 {t: NewPointer(I8), want: true}, 155 {t: I8, want: false}, 156 } 157 for _, g := range golden { 158 got := IsPointer(g.t) 159 if g.want != got { 160 t.Errorf("check if `%s` is a pointer type mismatch; expected %t, got %t", g.t, g.want, got) 161 } 162 } 163 } 164 165 func TestIsVector(t *testing.T) { 166 golden := []struct { 167 t Type 168 want bool 169 }{ 170 {t: &VectorType{Len: 5, ElemType: I8}, want: true}, 171 {t: NewVector(5, I8), want: true}, 172 {t: I8, want: false}, 173 } 174 for _, g := range golden { 175 got := IsVector(g.t) 176 if g.want != got { 177 t.Errorf("check if `%s` is a vector type mismatch; expected %t, got %t", g.t, g.want, got) 178 } 179 } 180 } 181 182 func TestIsLabel(t *testing.T) { 183 golden := []struct { 184 t Type 185 want bool 186 }{ 187 {t: &LabelType{}, want: true}, 188 {t: Label, want: true}, 189 {t: I8, want: false}, 190 } 191 for _, g := range golden { 192 got := IsLabel(g.t) 193 if g.want != got { 194 t.Errorf("check if `%s` is a label type mismatch; expected %t, got %t", g.t, g.want, got) 195 } 196 } 197 } 198 199 func TestIsToken(t *testing.T) { 200 golden := []struct { 201 t Type 202 want bool 203 }{ 204 {t: &TokenType{}, want: true}, 205 {t: Token, want: true}, 206 {t: I8, want: false}, 207 } 208 for _, g := range golden { 209 got := IsToken(g.t) 210 if g.want != got { 211 t.Errorf("check if `%s` is a token type mismatch; expected %t, got %t", g.t, g.want, got) 212 } 213 } 214 } 215 216 func TestIsMetadata(t *testing.T) { 217 golden := []struct { 218 t Type 219 want bool 220 }{ 221 {t: &MetadataType{}, want: true}, 222 {t: Metadata, want: true}, 223 {t: I8, want: false}, 224 } 225 for _, g := range golden { 226 got := IsMetadata(g.t) 227 if g.want != got { 228 t.Errorf("check if `%s` is a metadata type mismatch; expected %t, got %t", g.t, g.want, got) 229 } 230 } 231 } 232 233 func TestIsArray(t *testing.T) { 234 golden := []struct { 235 t Type 236 want bool 237 }{ 238 {t: &ArrayType{Len: 5, ElemType: I8}, want: true}, 239 {t: NewArray(5, I8), want: true}, 240 {t: I8, want: false}, 241 } 242 for _, g := range golden { 243 got := IsArray(g.t) 244 if g.want != got { 245 t.Errorf("check if `%s` is an array type mismatch; expected %t, got %t", g.t, g.want, got) 246 } 247 } 248 } 249 250 func TestIsStruct(t *testing.T) { 251 golden := []struct { 252 t Type 253 want bool 254 }{ 255 {t: &StructType{TypeName: "foo", Fields: []Type{I8}}, want: true}, 256 {t: &StructType{Fields: []Type{I8}}, want: true}, 257 {t: NewStruct(I8, I32), want: true}, 258 {t: NewStruct(I8), want: true}, 259 {t: NewStruct(), want: true}, 260 {t: I8, want: false}, 261 } 262 for _, g := range golden { 263 got := IsStruct(g.t) 264 if g.want != got { 265 t.Errorf("check if `%s` is an array type mismatch; expected %t, got %t", g.t, g.want, got) 266 } 267 } 268 } 269 270 func TestEqual(t *testing.T) { 271 golden := []struct { 272 t Type 273 u Type 274 want bool 275 }{ 276 {t: Void, u: &VoidType{}, want: true}, 277 {t: Void, u: I8, want: false}, 278 {t: NewFunc(Void), u: NewFunc(Void), want: true}, 279 {t: NewFunc(Void, I32), u: NewFunc(Void, I8), want: false}, 280 {t: &FuncType{RetType: Void, Variadic: true}, u: &FuncType{RetType: Void}, want: false}, 281 {t: NewFunc(Void), u: I8, want: false}, 282 {t: I8, u: NewInt(8), want: true}, 283 {t: I8, u: NewInt(9), want: false}, 284 {t: I8, u: Double, want: false}, 285 {t: &FloatType{Kind: FloatKindDouble}, u: Double, want: true}, 286 {t: Float, u: Double, want: false}, 287 {t: Float, u: I8, want: false}, 288 {t: MMX, u: &MMXType{}, want: true}, 289 {t: MMX, u: I8, want: false}, 290 {t: NewPointer(I8), u: &PointerType{ElemType: I8}, want: true}, 291 {t: NewPointer(I8), u: NewPointer(Double), want: false}, 292 {t: NewPointer(I8), u: I8, want: false}, 293 {t: NewVector(5, I8), u: &VectorType{Len: 5, ElemType: I8}, want: true}, 294 {t: NewVector(5, I8), u: NewVector(3, I8), want: false}, 295 {t: NewVector(5, I8), u: I8, want: false}, 296 {t: Label, u: &LabelType{}, want: true}, 297 {t: Label, u: I8, want: false}, 298 {t: Token, u: &TokenType{}, want: true}, 299 {t: Token, u: I8, want: false}, 300 {t: Metadata, u: &MetadataType{}, want: true}, 301 {t: Metadata, u: I8, want: false}, 302 {t: NewArray(5, I8), u: &ArrayType{Len: 5, ElemType: I8}, want: true}, 303 {t: NewArray(5, I8), u: NewArray(3, I8), want: false}, 304 {t: NewArray(5, I8), u: I8, want: false}, 305 } 306 for _, g := range golden { 307 got := Equal(g.t, g.u) 308 if g.want != got { 309 t.Errorf("equality mismatch between `%s` and `%s`; expected %t, got %t", g.t.LLString(), g.u.LLString(), g.want, got) 310 } 311 } 312 } 313 314 func TestStructTypeEqual(t *testing.T) { 315 // Identified (named) struct types are uniqued by type names, not by 316 // structural identity. 317 golden := []struct { 318 t *StructType 319 u *StructType 320 want bool 321 }{ 322 // Unnamed struct types. 323 { 324 t: &StructType{Fields: []Type{I32}}, 325 u: NewStruct(I8), 326 want: false, 327 }, 328 { 329 t: &StructType{Fields: []Type{I32}, Packed: false}, 330 u: &StructType{Fields: []Type{I32}, Packed: true}, 331 want: false, 332 }, 333 { 334 t: &StructType{Fields: []Type{I32}}, 335 u: &StructType{Fields: []Type{I32}}, 336 want: true, 337 }, 338 { 339 t: &StructType{Fields: []Type{I32}, Packed: true}, 340 u: &StructType{Fields: []Type{I32}, Packed: true}, 341 want: true, 342 }, 343 // Identified struct types. 344 { 345 t: &StructType{TypeName: "foo", Fields: []Type{I32}}, 346 u: &StructType{TypeName: "bar", Fields: []Type{I8}}, 347 want: false, 348 }, 349 { 350 t: &StructType{TypeName: "foo", Fields: []Type{I32}, Packed: false}, 351 u: &StructType{TypeName: "bar", Fields: []Type{I32}, Packed: true}, 352 want: false, 353 }, 354 { 355 t: &StructType{TypeName: "foo", Fields: []Type{I32}}, 356 u: &StructType{TypeName: "foo", Fields: []Type{I32}}, 357 want: true, 358 }, 359 { 360 t: &StructType{TypeName: "foo", Fields: []Type{I32}, Packed: true}, 361 u: &StructType{TypeName: "foo", Fields: []Type{I32}, Packed: true}, 362 want: true, 363 }, 364 { 365 t: &StructType{TypeName: "foo", Fields: []Type{I32}}, 366 u: &StructType{TypeName: "bar", Fields: []Type{I32}}, 367 want: false, 368 }, 369 { 370 t: &StructType{TypeName: "foo", Fields: []Type{I32}, Packed: true}, 371 u: &StructType{TypeName: "bar", Fields: []Type{I32}, Packed: true}, 372 want: false, 373 }, 374 } 375 for _, g := range golden { 376 got := g.t.Equal(g.u) 377 if g.want != got { 378 t.Errorf("struct equality mismatch between `%s` and `%s`; expected %t, got %t", g.t.LLString(), g.u.LLString(), g.want, got) 379 } 380 } 381 } 382 383 // Assert that each type implements the types.Type interface. 384 var ( 385 _ Type = (*VoidType)(nil) 386 _ Type = (*FuncType)(nil) 387 _ Type = (*IntType)(nil) 388 _ Type = (*FloatType)(nil) 389 _ Type = (*MMXType)(nil) 390 _ Type = (*PointerType)(nil) 391 _ Type = (*VectorType)(nil) 392 _ Type = (*LabelType)(nil) 393 _ Type = (*TokenType)(nil) 394 _ Type = (*MetadataType)(nil) 395 _ Type = (*ArrayType)(nil) 396 _ Type = (*StructType)(nil) 397 )