blob: b2ad25f0606878eabd5a7078118edfff3e578b45 [file] [log] [blame]
Bram Moolenaar29f9ed22018-04-10 19:20:31 +02001" Test for the xxd command
2if empty($XXD) || !executable($XXD)
3 finish
4endif
5
Bram Moolenaar164268d2018-04-10 20:06:17 +02006func! PrepareBuffer(lines)
7 new
8 call append(0, a:lines)
Bram Moolenaar29f9ed22018-04-10 19:20:31 +02009 $d
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020010endfunc
11
12func! s:Mess(counter)
13 return printf("Failed xxd test %d:", a:counter)
14endfunc
15
16func! Test_xxd()
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020017 call PrepareBuffer(range(1,30))
Bram Moolenaar164268d2018-04-10 20:06:17 +020018 w XXDfile
19
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020020 " Test 1: simple, filter the result through xxd
21 let s:test = 1
22 %!$XXD %
23 let expected = [
24 \ '00000000: 310a 320a 330a 340a 350a 360a 370a 380a 1.2.3.4.5.6.7.8.',
25 \ '00000010: 390a 3130 0a31 310a 3132 0a31 330a 3134 9.10.11.12.13.14',
26 \ '00000020: 0a31 350a 3136 0a31 370a 3138 0a31 390a .15.16.17.18.19.',
27 \ '00000030: 3230 0a32 310a 3232 0a32 330a 3234 0a32 20.21.22.23.24.2',
28 \ '00000040: 350a 3236 0a32 370a 3238 0a32 390a 3330 5.26.27.28.29.30',
29 \ '00000050: 0a .']
30 call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
Bram Moolenaar164268d2018-04-10 20:06:17 +020031
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020032 " Test 2: reverse the result
33 let s:test += 1
34 %!$XXD -r
35 call assert_equal(map(range(1,30), {v,c -> string(c)}), getline(1,'$'), s:Mess(s:test))
36
37 " Test 3: Skip the first 30 bytes
38 let s:test += 1
39 %!$XXD -s 0x30 %
40 call assert_equal(expected[3:], getline(1,'$'), s:Mess(s:test))
41
42 " Test 4: Skip the first 30 bytes
43 let s:test += 1
44 %!$XXD -s -0x31 %
45 call assert_equal(expected[2:], getline(1,'$'), s:Mess(s:test))
46
47 " Test 5: Print 120 bytes as continuous hexdump with 20 octets per line
48 let s:test += 1
49 %d
50 0r! $XXD -l 120 -ps -c 20 ../../runtime/doc/xxd.1
51 $d
52 let expected = [
53 \ '2e54482058584420312022417567757374203139',
54 \ '39362220224d616e75616c207061676520666f72',
55 \ '20787864220a2e5c220a2e5c222032317374204d',
56 \ '617920313939360a2e5c22204d616e2070616765',
57 \ '20617574686f723a0a2e5c2220202020546f6e79',
58 \ '204e7567656e74203c746f6e79407363746e7567']
59 call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
Bram Moolenaar164268d2018-04-10 20:06:17 +020060
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020061 " Test 6: Print the date from xxd.1
62 let s:test += 1
63 %d
64 0r! $XXD -s 0x36 -l 13 -c 13 ../../runtime/doc/xxd.1
65 $d
66 call assert_equal('00000036: 3231 7374 204d 6179 2031 3939 36 21st May 1996', getline(1), s:Mess(s:test))
Bram Moolenaar164268d2018-04-10 20:06:17 +020067
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020068 " Test 7: Print C include
69 let s:test += 1
70 call writefile(['TESTabcd09'], 'XXDfile')
71 %d
72 0r! $XXD -i XXDfile
73 $d
74 let expected = ['unsigned char XXDfile[] = {',
75 \ ' 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a', '};',
76 \ 'unsigned int XXDfile_len = 11;']
77 call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
Bram Moolenaar164268d2018-04-10 20:06:17 +020078
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020079 " Test 8: Print C include capitalized
80 let s:test += 1
81 call writefile(['TESTabcd09'], 'XXDfile')
82 %d
83 0r! $XXD -i -C XXDfile
84 $d
85 let expected = ['unsigned char XXDFILE[] = {',
86 \ ' 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a', '};',
87 \ 'unsigned int XXDFILE_LEN = 11;']
88 call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
Bram Moolenaar164268d2018-04-10 20:06:17 +020089
90 " Test 9: Create a file with containing a single 'A'
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020091 let s:test += 1
92 call delete('XXDfile')
Bram Moolenaar164268d2018-04-10 20:06:17 +020093 bwipe! XXDfile
Bram Moolenaar29f9ed22018-04-10 19:20:31 +020094 call system('echo "010000: 41"|'.$XXD.' -r -s -0x10000 > XXDfile')
95 call PrepareBuffer(readfile('XXDfile')[0])
96 call assert_equal('A', getline(1), s:Mess(s:test))
97 call delete('XXDfile')
98 %d
99 bw!
100endfunc