github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/transpiler/literals_test.go (about) 1 package transpiler 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 "unicode/utf8" 8 9 goast "go/ast" 10 "go/token" 11 12 "github.com/Konstantin8105/c4go/ast" 13 ) 14 15 var chartests = []struct { 16 in int // Integer Character Code 17 out string // Output Character Literal 18 }{ 19 // NUL byte 20 {0, "'\\x00'"}, 21 22 // ASCII control characters 23 {7, "'\\a'"}, 24 {8, "'\\b'"}, 25 {9, "'\\t'"}, 26 {10, "'\\n'"}, 27 {11, "'\\v'"}, 28 {12, "'\\f'"}, 29 {13, "'\\r'"}, 30 31 // printable ASCII 32 {32, "' '"}, 33 {34, "'\"'"}, 34 {39, "'\\''"}, 35 {65, "'A'"}, 36 {92, "'\\\\'"}, 37 {191, "'¿'"}, 38 39 // printable unicode 40 {948, "'δ'"}, 41 {0x03a9, "'Ω'"}, 42 {0x2020, "'†'"}, 43 44 // non-printable unicode 45 {0xffff, "'\\uffff'"}, 46 {utf8.MaxRune, "'\\U0010ffff'"}, 47 } 48 49 func TestCharacterLiterals(t *testing.T) { 50 for _, tt := range chartests { 51 expected := &goast.BasicLit{Kind: token.CHAR, Value: tt.out} 52 actual := transpileCharacterLiteral(&ast.CharacterLiteral{Value: tt.in}) 53 if !reflect.DeepEqual(expected, actual) { 54 t.Errorf("input: %v", tt.in) 55 t.Errorf(" expected: %v", expected) 56 t.Errorf(" actual: %v", actual) 57 } 58 } 59 } 60 61 func TestFormatFlag(t *testing.T) { 62 tcs := []struct { 63 in, out string 64 }{ 65 { 66 in: "", 67 out: "", 68 }, 69 { 70 in: "%", 71 out: "%", 72 }, 73 { 74 in: "%34", 75 out: "%34", 76 }, 77 { 78 in: "%5.4", 79 out: "%5.4", 80 }, 81 { 82 in: "%5f", 83 out: "%5f", 84 }, 85 { 86 in: "%u %u", 87 out: "%d %d", 88 }, 89 { 90 in: "%u %2u", 91 out: "%d %2d", 92 }, 93 { 94 in: "%u", 95 out: "%d", 96 }, 97 { 98 in: "%5u %2u", 99 out: "%5d %2d", 100 }, 101 { 102 in: "%5u", 103 out: "%5d", 104 }, 105 { 106 in: "%lf", 107 out: "%f", 108 }, 109 { 110 in: "%12lf", 111 out: "%12f", 112 }, 113 { 114 in: "%12.4lf", 115 out: "%12.4f", 116 }, 117 } 118 119 for _, tc := range tcs { 120 t.Run(fmt.Sprintf("%v", tc.in), func(t *testing.T) { 121 act := ConvertToGoFlagFormat(tc.in) 122 if act != tc.out { 123 t.Fatalf("Not same '%v' != '%v'", act, tc.out) 124 } 125 }) 126 } 127 }