github.com/llir/llvm@v0.3.6/internal/enc/enc_test.go (about)

     1  package enc
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestGlobalName(t *testing.T) {
     9  	golden := []struct {
    10  		s    string
    11  		want string
    12  	}{
    13  		// i=0
    14  		{s: "foo", want: "@foo"},
    15  		// i=1
    16  		{s: "a b", want: `@"a b"`},
    17  		// i=2
    18  		{s: "$a", want: "@$a"},
    19  		// i=3
    20  		{s: "-a", want: "@-a"},
    21  		// i=4
    22  		{s: ".a", want: "@.a"},
    23  		// i=5
    24  		{s: "_a", want: "@_a"},
    25  		// i=6
    26  		{s: "#a", want: `@"#a"`},
    27  		// i=7
    28  		{s: "a b#c", want: `@"a b#c"`},
    29  		// i=8
    30  		{s: "2", want: `@"2"`},
    31  		// i=9
    32  		{s: "foo世bar", want: `@"foo\E4\B8\96bar"`},
    33  		// i=10
    34  		{s: "foo\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA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\F7\\F8\\F9\\FA\\FB\\FC\\FD\\FE\\FF\""},
    35  	}
    36  	for i, g := range golden {
    37  		got := GlobalName(g.s)
    38  		if g.want != got {
    39  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
    40  		}
    41  	}
    42  }
    43  
    44  func TestLocalName(t *testing.T) {
    45  	golden := []struct {
    46  		s    string
    47  		want string
    48  	}{
    49  		// i=0
    50  		{s: "foo", want: "%foo"},
    51  		// i=1
    52  		{s: "a b", want: `%"a b"`},
    53  		// i=2
    54  		{s: "$a", want: "%$a"},
    55  		// i=3
    56  		{s: "-a", want: "%-a"},
    57  		// i=4
    58  		{s: ".a", want: "%.a"},
    59  		// i=5
    60  		{s: "_a", want: "%_a"},
    61  		// i=6
    62  		{s: "#a", want: `%"#a"`},
    63  		// i=7
    64  		{s: "a b#c", want: `%"a b#c"`},
    65  		// i=8
    66  		{s: "2", want: `%"2"`},
    67  		// i=9
    68  		{s: "foo世bar", want: `%"foo\E4\B8\96bar"`},
    69  		// i=10
    70  		{s: "foo\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F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\\E5\\E6\\E7\\E8\\E9\\EA\\EB\\EC\\ED\\EE\\EF\\F0\\F1\\F2\\F3\\F4\\F5\\F6\\F7\\F8\\F9\\FA\\FB\\FC\\FD\\FE\\FF\""},
    71  	}
    72  	for i, g := range golden {
    73  		got := LocalName(g.s)
    74  		if g.want != got {
    75  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
    76  		}
    77  	}
    78  }
    79  
    80  func TestLabelName(t *testing.T) {
    81  	golden := []struct {
    82  		s    string
    83  		want string
    84  	}{
    85  		// i=0
    86  		{s: "foo", want: "foo:"},
    87  		// i=1
    88  		{s: "a b", want: `"a b":`},
    89  		// i=2
    90  		{s: "$a", want: "$a:"},
    91  		// i=3
    92  		{s: "-a", want: "-a:"},
    93  		// i=4
    94  		{s: ".a", want: ".a:"},
    95  		// i=5
    96  		{s: "_a", want: "_a:"},
    97  		// i=6
    98  		{s: "#a", want: `"#a":`},
    99  		// i=7
   100  		{s: "a b#c", want: `"a b#c":`},
   101  		// i=8
   102  		{s: "2", want: `"2":`},
   103  		// i=9
   104  		{s: "foo世bar", want: `"foo\E4\B8\96bar":`},
   105  		// i=10
   106  		{s: "foo\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7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D4\\D5\\D6\\D7\\D8\\D9\\DA\\DB\\DC\\DD\\DE\\DF\\E0\\E1\\E2\\E3\\E4\\E5\\E6\\E7\\E8\\E9\\EA\\EB\\EC\\ED\\EE\\EF\\F0\\F1\\F2\\F3\\F4\\F5\\F6\\F7\\F8\\F9\\FA\\FB\\FC\\FD\\FE\\FF\":"},
   107  	}
   108  	for i, g := range golden {
   109  		got := LabelName(g.s)
   110  		if g.want != got {
   111  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
   112  		}
   113  	}
   114  }
   115  
   116  func TestTypeName(t *testing.T) {
   117  	golden := []struct {
   118  		s    string
   119  		want string
   120  	}{
   121  		// i=0
   122  		{s: "foo", want: "%foo"},
   123  		// i=1
   124  		{s: "a b", want: `%"a b"`},
   125  		// i=2
   126  		{s: "$a", want: "%$a"},
   127  		// i=3
   128  		{s: "-a", want: "%-a"},
   129  		// i=4
   130  		{s: ".a", want: "%.a"},
   131  		// i=5
   132  		{s: "_a", want: "%_a"},
   133  		// i=6
   134  		{s: "#a", want: `%"#a"`},
   135  		// i=7
   136  		{s: "a b#c", want: `%"a b#c"`},
   137  		// i=8
   138  		{s: "2", want: `%2`}, // the only difference between enc.LocalName and enc.TypeName
   139  		// i=9
   140  		{s: "foo世bar", want: `%"foo\E4\B8\96bar"`},
   141  		// i=10
   142  		{s: "foo\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60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\BD\\BE\\BF\\C0\\C1\\C2\\C3\\C4\\C5\\C6\\C7\\C8\\C9\\CA\\CB\\CC\\CD\\CE\\CF\\D0\\D1\\D2\\D3\\D4\\D5\\D6\\D7\\D8\\D9\\DA\\DB\\DC\\DD\\DE\\DF\\E0\\E1\\E2\\E3\\E4\\E5\\E6\\E7\\E8\\E9\\EA\\EB\\EC\\ED\\EE\\EF\\F0\\F1\\F2\\F3\\F4\\F5\\F6\\F7\\F8\\F9\\FA\\FB\\FC\\FD\\FE\\FF\""},
   143  	}
   144  	for i, g := range golden {
   145  		got := TypeName(g.s)
   146  		if g.want != got {
   147  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
   148  		}
   149  	}
   150  }
   151  
   152  func TestAttrGroupID(t *testing.T) {
   153  	golden := []struct {
   154  		s    int64
   155  		want string
   156  	}{
   157  		// i=0
   158  		{s: 42, want: "#42"},
   159  	}
   160  	for i, g := range golden {
   161  		got := AttrGroupID(g.s)
   162  		if g.want != got {
   163  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
   164  		}
   165  	}
   166  }
   167  
   168  func TestComdatName(t *testing.T) {
   169  	golden := []struct {
   170  		s    string
   171  		want string
   172  	}{
   173  		// i=0
   174  		{s: "foo", want: "$foo"},
   175  		// i=1
   176  		{s: "a b", want: `$"a b"`},
   177  		// i=2
   178  		{s: "$a", want: "$$a"},
   179  		// i=3
   180  		{s: "-a", want: "$-a"},
   181  		// i=4
   182  		{s: ".a", want: "$.a"},
   183  		// i=5
   184  		{s: "_a", want: "$_a"},
   185  		// i=6
   186  		{s: "#a", want: `$"#a"`},
   187  		// i=7
   188  		{s: "a b#c", want: `$"a b#c"`},
   189  		// i=8
   190  		{s: "foo世bar", want: `$"foo\E4\B8\96bar"`},
   191  		// i=9
   192  		{s: "foo\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF", want: "$\"foo\\01\\02\\03\\04\\05\\06\\07\\08\\09\\0A\\0B\\0C\\0D\\0E\\0F\\10\\11\\12\\13\\14\\15\\16\\17\\18\\19\\1A\\1B\\1C\\1D\\1E\\1F !\\22#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\5C]^_`abcdefghijklmnopqrstuvwxyz{|}~\\7F\\80\\81\\82\\83\\84\\85\\86\\87\\88\\89\\8A\\8B\\8C\\8D\\8E\\8F\\90\\91\\92\\93\\94\\95\\96\\97\\98\\99\\9A\\9B\\9C\\9D\\9E\\9F\\A0\\A1\\A2\\A3\\A4\\A5\\A6\\A7\\A8\\A9\\AA\\AB\\AC\\AD\\AE\\AF\\B0\\B1\\B2\\B3\\B4\\B5\\B6\\B7\\B8\\B9\\BA\\BB\\BC\\BD\\BE\\BF\\C0\\C1\\C2\\C3\\C4\\C5\\C6\\C7\\C8\\C9\\CA\\CB\\CC\\CD\\CE\\CF\\D0\\D1\\D2\\D3\\D4\\D5\\D6\\D7\\D8\\D9\\DA\\DB\\DC\\DD\\DE\\DF\\E0\\E1\\E2\\E3\\E4\\E5\\E6\\E7\\E8\\E9\\EA\\EB\\EC\\ED\\EE\\EF\\F0\\F1\\F2\\F3\\F4\\F5\\F6\\F7\\F8\\F9\\FA\\FB\\FC\\FD\\FE\\FF\""},
   193  	}
   194  	for i, g := range golden {
   195  		got := ComdatName(g.s)
   196  		if g.want != got {
   197  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
   198  		}
   199  	}
   200  }
   201  
   202  func TestMetadataName(t *testing.T) {
   203  	golden := []struct {
   204  		s    string
   205  		want string
   206  	}{
   207  		// i=0
   208  		{s: "foo", want: "!foo"},
   209  		// i=1
   210  		{s: "a b", want: `!a\20b`},
   211  		// i=2
   212  		{s: "$a", want: "!$a"},
   213  		// i=3
   214  		{s: "-a", want: "!-a"},
   215  		// i=4
   216  		{s: ".a", want: "!.a"},
   217  		// i=5
   218  		{s: "_a", want: "!_a"},
   219  		// i=6
   220  		{s: "#a", want: `!\23a`},
   221  		// i=7
   222  		{s: "a b#c", want: `!a\20b\23c`},
   223  		// i=8
   224  		{s: "foo世bar", want: `!foo\E4\B8\96bar`},
   225  		// i=9
   226  		{s: "qux\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF", want: `!qux\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F\10\11\12\13\14\15\16\17\18\19\1A\1B\1C\1D\1E\1F\20\21\22\23$\25\26\27\28\29\2A\2B\2C-.\2F0123456789\3A\3B\3C\3D\3E\3F\40ABCDEFGHIJKLMNOPQRSTUVWXYZ\5B\5C\5D\5E_\60abcdefghijklmnopqrstuvwxyz\7B\7C\7D\7E\7F\80\81\82\83\84\85\86\87\88\89\8A\8B\8C\8D\8E\8F\90\91\92\93\94\95\96\97\98\99\9A\9B\9C\9D\9E\9F\A0\A1\A2\A3\A4\A5\A6\A7\A8\A9\AA\AB\AC\AD\AE\AF\B0\B1\B2\B3\B4\B5\B6\B7\B8\B9\BA\BB\BC\BD\BE\BF\C0\C1\C2\C3\C4\C5\C6\C7\C8\C9\CA\CB\CC\CD\CE\CF\D0\D1\D2\D3\D4\D5\D6\D7\D8\D9\DA\DB\DC\DD\DE\DF\E0\E1\E2\E3\E4\E5\E6\E7\E8\E9\EA\EB\EC\ED\EE\EF\F0\F1\F2\F3\F4\F5\F6\F7\F8\F9\FA\FB\FC\FD\FE\FF`},
   227  		// i=10
   228  		{s: "123", want: `!\3123`},
   229  	}
   230  	for i, g := range golden {
   231  		got := MetadataName(g.s)
   232  		if g.want != got {
   233  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
   234  		}
   235  	}
   236  }
   237  
   238  func TestMetadataID(t *testing.T) {
   239  	golden := []struct {
   240  		s    int64
   241  		want string
   242  	}{
   243  		// i=0
   244  		{s: 2, want: "!2"},
   245  		// i=1
   246  		{s: 42, want: "!42"},
   247  	}
   248  	for i, g := range golden {
   249  		got := MetadataID(g.s)
   250  		if g.want != got {
   251  			t.Errorf("i=%d: name mismatch; expected `%s`, got `%s`", i, g.want, got)
   252  		}
   253  	}
   254  }
   255  
   256  func TestEscapeString(t *testing.T) {
   257  	golden := []struct {
   258  		s    []byte
   259  		want string
   260  	}{
   261  		// i=0
   262  		{s: []byte("foo"), want: "foo"},
   263  		// i=1
   264  		{s: []byte("a b"), want: `a b`},
   265  		// i=2
   266  		{s: []byte("$a"), want: "$a"},
   267  		// i=3
   268  		{s: []byte("-a"), want: "-a"},
   269  		// i=4
   270  		{s: []byte(".a"), want: ".a"},
   271  		// i=5
   272  		{s: []byte("_a"), want: "_a"},
   273  		// i=6
   274  		{s: []byte("#a"), want: `#a`},
   275  		// i=7
   276  		{s: []byte("a b#c"), want: `a b#c`},
   277  		// i=8
   278  		{s: []byte("2"), want: "2"},
   279  		// i=9
   280  		{s: []byte("foo世bar"), want: `foo\E4\B8\96bar`},
   281  		// i=10
   282  		{s: []byte(`foo \ bar`), want: `foo \5C bar`},
   283  		// i=11 (arbitrary data, invalid UTF-8)
   284  		{s: []byte{'f', 'o', 'o', 0x81, 0x82, 'b', 'a', 'r'}, want: `foo\81\82bar`},
   285  	}
   286  	for i, g := range golden {
   287  		got := EscapeString(g.s)
   288  		if g.want != got {
   289  			t.Errorf("i=%d: string mismatch; expected `%s`, got `%s`", i, g.want, got)
   290  		}
   291  	}
   292  }
   293  
   294  func TestEscape(t *testing.T) {
   295  	golden := []struct {
   296  		s    []byte
   297  		want string
   298  	}{
   299  		// i=0
   300  		{s: []byte("foo"), want: "foo"},
   301  		// i=1
   302  		{s: []byte("a b"), want: `a b`},
   303  		// i=2
   304  		{s: []byte("$a"), want: "$a"},
   305  		// i=3
   306  		{s: []byte("-a"), want: "-a"},
   307  		// i=4
   308  		{s: []byte(".a"), want: ".a"},
   309  		// i=5
   310  		{s: []byte("_a"), want: "_a"},
   311  		// i=6
   312  		{s: []byte("#a"), want: `#a`},
   313  		// i=7
   314  		{s: []byte("a b#c"), want: `a b#c`},
   315  		// i=8
   316  		{s: []byte("2"), want: "2"},
   317  		// i=9
   318  		{s: []byte("foo世bar"), want: `foo\E4\B8\96bar`},
   319  		// i=10
   320  		{s: []byte(`foo \ bar`), want: `foo \5C bar`},
   321  		// i=11 (arbitrary data, invalid UTF-8)
   322  		{s: []byte{'f', 'o', 'o', 0x81, 0x82, 'b', 'a', 'r'}, want: `foo\81\82bar`},
   323  	}
   324  	// isPrint reports whether the given byte is printable in ASCII.
   325  	isPrint := func(b byte) bool {
   326  		return ' ' <= b && b <= '~' && b != '"' && b != '\\'
   327  	}
   328  	for i, g := range golden {
   329  		got := Escape(g.s, isPrint)
   330  		if g.want != got {
   331  			t.Errorf("i=%d: string mismatch; expected `%s`, got `%s`", i, g.want, got)
   332  		}
   333  	}
   334  }
   335  
   336  func TestUnescape(t *testing.T) {
   337  	golden := []struct {
   338  		s    string
   339  		want []byte
   340  	}{
   341  		// i=0
   342  		{s: "foo", want: []byte("foo")},
   343  		// i=1
   344  		{s: `a\20b`, want: []byte("a b")},
   345  		// i=2
   346  		{s: "$a", want: []byte("$a")},
   347  		// i=3
   348  		{s: "-a", want: []byte("-a")},
   349  		// i=4
   350  		{s: ".a", want: []byte(".a")},
   351  		// i=5
   352  		{s: "_a", want: []byte("_a")},
   353  		// i=6
   354  		{s: `\23a`, want: []byte("#a")},
   355  		// i=7
   356  		{s: `a\20b\23c`, want: []byte("a b#c")},
   357  		// i=8
   358  		{s: "2", want: []byte("2")},
   359  		// i=9
   360  		{s: `foo\E4\B8\96bar`, want: []byte("foo世bar")},
   361  		// i=10
   362  		{s: `foo \5C bar`, want: []byte(`foo \ bar`)},
   363  		// i=11
   364  		{s: `foo \5c bar`, want: []byte(`foo \ bar`)},
   365  		// i=12
   366  		{s: `foo \\ bar`, want: []byte(`foo \ bar`)},
   367  		// i=13 (arbitrary data, invalid UTF-8)
   368  		{s: `foo\81\82bar`, want: []byte{'f', 'o', 'o', 0x81, 0x82, 'b', 'a', 'r'}},
   369  	}
   370  	for i, g := range golden {
   371  		got := Unescape(g.s)
   372  		if !reflect.DeepEqual(g.want, got) {
   373  			t.Errorf("i=%d: string mismatch; expected `%s`, got `%s`", i, g.want, got)
   374  		}
   375  	}
   376  }
   377  
   378  func TestQuote(t *testing.T) {
   379  	golden := []struct {
   380  		s    []byte
   381  		want string
   382  	}{
   383  		// i=0
   384  		{s: []byte("foo"), want: `"foo"`},
   385  		// i=1
   386  		{s: []byte("a b"), want: `"a b"`},
   387  		// i=2
   388  		{s: []byte("$a"), want: `"$a"`},
   389  		// i=3
   390  		{s: []byte("-a"), want: `"-a"`},
   391  		// i=4
   392  		{s: []byte(".a"), want: `".a"`},
   393  		// i=5
   394  		{s: []byte("_a"), want: `"_a"`},
   395  		// i=6
   396  		{s: []byte("#a"), want: `"#a"`},
   397  		// i=7
   398  		{s: []byte("a b#c"), want: `"a b#c"`},
   399  		// i=8
   400  		{s: []byte("2"), want: `"2"`},
   401  		// i=9
   402  		{s: []byte("foo世bar"), want: `"foo\E4\B8\96bar"`},
   403  		// i=10
   404  		{s: []byte(`foo \ bar`), want: `"foo \5C bar"`},
   405  		// i=11 (arbitrary data, invalid UTF-8)
   406  		{s: []byte{'f', 'o', 'o', 0x81, 0x82, 'b', 'a', 'r'}, want: `"foo\81\82bar"`},
   407  	}
   408  	for i, g := range golden {
   409  		got := Quote(g.s)
   410  		if g.want != got {
   411  			t.Errorf("i=%d: string mismatch; expected `%s`, got `%s`", i, g.want, got)
   412  		}
   413  	}
   414  }
   415  
   416  func TestUnquote(t *testing.T) {
   417  	golden := []struct {
   418  		s    string
   419  		want []byte
   420  	}{
   421  		// i=0
   422  		{s: `"foo"`, want: []byte("foo")},
   423  		// i=1
   424  		{s: `"a\20b"`, want: []byte("a b")},
   425  		// i=2
   426  		{s: `"$a"`, want: []byte("$a")},
   427  		// i=3
   428  		{s: `"-a"`, want: []byte("-a")},
   429  		// i=4
   430  		{s: `".a"`, want: []byte(".a")},
   431  		// i=5
   432  		{s: `"_a"`, want: []byte("_a")},
   433  		// i=6
   434  		{s: `"\23a"`, want: []byte("#a")},
   435  		// i=7
   436  		{s: `"a\20b\23c"`, want: []byte("a b#c")},
   437  		// i=8
   438  		{s: `"2"`, want: []byte("2")},
   439  		// i=9
   440  		{s: `"foo\E4\B8\96bar"`, want: []byte("foo世bar")},
   441  		// i=10
   442  		{s: `"foo \5C bar"`, want: []byte(`foo \ bar`)},
   443  		// i=11
   444  		{s: `"foo \\ bar"`, want: []byte(`foo \ bar`)},
   445  		// i=12 (arbitrary data, invalid UTF-8)
   446  		{s: `"foo\81\82bar"`, want: []byte{'f', 'o', 'o', 0x81, 0x82, 'b', 'a', 'r'}},
   447  		// i=13
   448  		{s: `"\8E\\"`, want: []byte{0x8E, '\\'}}, // see issue llir/llvm#184
   449  		// i=14
   450  		{s: `"\\"`, want: []byte{'\\'}},
   451  	}
   452  	for i, g := range golden {
   453  		got := Unquote(g.s)
   454  		if !reflect.DeepEqual(g.want, got) {
   455  			t.Errorf("i=%d: string mismatch; expected `%s`, got `%s`", i, g.want, got)
   456  		}
   457  	}
   458  }
   459  
   460  func BenchmarkGlobalNoReplace(b *testing.B) {
   461  	for i := 0; i < b.N; i++ {
   462  		GlobalName("$foo_bar_baz")
   463  	}
   464  }
   465  
   466  func BenchmarkGlobalReplace(b *testing.B) {
   467  	for i := 0; i < b.N; i++ {
   468  		GlobalName("$foo bar#baz")
   469  	}
   470  }
   471  
   472  func BenchmarkLocalNoReplace(b *testing.B) {
   473  	for i := 0; i < b.N; i++ {
   474  		LocalName("$foo_bar_baz")
   475  	}
   476  }
   477  
   478  func BenchmarkLocalReplace(b *testing.B) {
   479  	for i := 0; i < b.N; i++ {
   480  		LocalName("$foo bar#baz")
   481  	}
   482  }