How to use Return method of got Package

Best Got code snippet using got.Return

Run Got automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

shift_test.go

Source: shift_test.go Github

copy
1// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package gc
6
7import (
8	"reflect"
9	"testing"
10)
11
12// Tests shifts of zero.
13
14//go:noinline
15func ofz64l64(n uint64) int64 {
16	var x int64
17	return x << n
18}
19
20//go:noinline
21func ofz64l32(n uint32) int64 {
22	var x int64
23	return x << n
24}
25
26//go:noinline
27func ofz64l16(n uint16) int64 {
28	var x int64
29	return x << n
30}
31
32//go:noinline
33func ofz64l8(n uint8) int64 {
34	var x int64
35	return x << n
36}
37
38//go:noinline
39func ofz64r64(n uint64) int64 {
40	var x int64
41	return x >> n
42}
43
44//go:noinline
45func ofz64r32(n uint32) int64 {
46	var x int64
47	return x >> n
48}
49
50//go:noinline
51func ofz64r16(n uint16) int64 {
52	var x int64
53	return x >> n
54}
55
56//go:noinline
57func ofz64r8(n uint8) int64 {
58	var x int64
59	return x >> n
60}
61
62//go:noinline
63func ofz64ur64(n uint64) uint64 {
64	var x uint64
65	return x >> n
66}
67
68//go:noinline
69func ofz64ur32(n uint32) uint64 {
70	var x uint64
71	return x >> n
72}
73
74//go:noinline
75func ofz64ur16(n uint16) uint64 {
76	var x uint64
77	return x >> n
78}
79
80//go:noinline
81func ofz64ur8(n uint8) uint64 {
82	var x uint64
83	return x >> n
84}
85
86//go:noinline
87func ofz32l64(n uint64) int32 {
88	var x int32
89	return x << n
90}
91
92//go:noinline
93func ofz32l32(n uint32) int32 {
94	var x int32
95	return x << n
96}
97
98//go:noinline
99func ofz32l16(n uint16) int32 {
100	var x int32
101	return x << n
102}
103
104//go:noinline
105func ofz32l8(n uint8) int32 {
106	var x int32
107	return x << n
108}
109
110//go:noinline
111func ofz32r64(n uint64) int32 {
112	var x int32
113	return x >> n
114}
115
116//go:noinline
117func ofz32r32(n uint32) int32 {
118	var x int32
119	return x >> n
120}
121
122//go:noinline
123func ofz32r16(n uint16) int32 {
124	var x int32
125	return x >> n
126}
127
128//go:noinline
129func ofz32r8(n uint8) int32 {
130	var x int32
131	return x >> n
132}
133
134//go:noinline
135func ofz32ur64(n uint64) uint32 {
136	var x uint32
137	return x >> n
138}
139
140//go:noinline
141func ofz32ur32(n uint32) uint32 {
142	var x uint32
143	return x >> n
144}
145
146//go:noinline
147func ofz32ur16(n uint16) uint32 {
148	var x uint32
149	return x >> n
150}
151
152//go:noinline
153func ofz32ur8(n uint8) uint32 {
154	var x uint32
155	return x >> n
156}
157
158//go:noinline
159func ofz16l64(n uint64) int16 {
160	var x int16
161	return x << n
162}
163
164//go:noinline
165func ofz16l32(n uint32) int16 {
166	var x int16
167	return x << n
168}
169
170//go:noinline
171func ofz16l16(n uint16) int16 {
172	var x int16
173	return x << n
174}
175
176//go:noinline
177func ofz16l8(n uint8) int16 {
178	var x int16
179	return x << n
180}
181
182//go:noinline
183func ofz16r64(n uint64) int16 {
184	var x int16
185	return x >> n
186}
187
188//go:noinline
189func ofz16r32(n uint32) int16 {
190	var x int16
191	return x >> n
192}
193
194//go:noinline
195func ofz16r16(n uint16) int16 {
196	var x int16
197	return x >> n
198}
199
200//go:noinline
201func ofz16r8(n uint8) int16 {
202	var x int16
203	return x >> n
204}
205
206//go:noinline
207func ofz16ur64(n uint64) uint16 {
208	var x uint16
209	return x >> n
210}
211
212//go:noinline
213func ofz16ur32(n uint32) uint16 {
214	var x uint16
215	return x >> n
216}
217
218//go:noinline
219func ofz16ur16(n uint16) uint16 {
220	var x uint16
221	return x >> n
222}
223
224//go:noinline
225func ofz16ur8(n uint8) uint16 {
226	var x uint16
227	return x >> n
228}
229
230//go:noinline
231func ofz8l64(n uint64) int8 {
232	var x int8
233	return x << n
234}
235
236//go:noinline
237func ofz8l32(n uint32) int8 {
238	var x int8
239	return x << n
240}
241
242//go:noinline
243func ofz8l16(n uint16) int8 {
244	var x int8
245	return x << n
246}
247
248//go:noinline
249func ofz8l8(n uint8) int8 {
250	var x int8
251	return x << n
252}
253
254//go:noinline
255func ofz8r64(n uint64) int8 {
256	var x int8
257	return x >> n
258}
259
260//go:noinline
261func ofz8r32(n uint32) int8 {
262	var x int8
263	return x >> n
264}
265
266//go:noinline
267func ofz8r16(n uint16) int8 {
268	var x int8
269	return x >> n
270}
271
272//go:noinline
273func ofz8r8(n uint8) int8 {
274	var x int8
275	return x >> n
276}
277
278//go:noinline
279func ofz8ur64(n uint64) uint8 {
280	var x uint8
281	return x >> n
282}
283
284//go:noinline
285func ofz8ur32(n uint32) uint8 {
286	var x uint8
287	return x >> n
288}
289
290//go:noinline
291func ofz8ur16(n uint16) uint8 {
292	var x uint8
293	return x >> n
294}
295
296//go:noinline
297func ofz8ur8(n uint8) uint8 {
298	var x uint8
299	return x >> n
300}
301
302func TestShiftOfZero(t *testing.T) {
303	if got := ofz64l64(5); got != 0 {
304		t.Errorf("0<<5 == %d, want 0", got)
305	}
306	if got := ofz64l32(5); got != 0 {
307		t.Errorf("0<<5 == %d, want 0", got)
308	}
309	if got := ofz64l16(5); got != 0 {
310		t.Errorf("0<<5 == %d, want 0", got)
311	}
312	if got := ofz64l8(5); got != 0 {
313		t.Errorf("0<<5 == %d, want 0", got)
314	}
315	if got := ofz64r64(5); got != 0 {
316		t.Errorf("0>>5 == %d, want 0", got)
317	}
318	if got := ofz64r32(5); got != 0 {
319		t.Errorf("0>>5 == %d, want 0", got)
320	}
321	if got := ofz64r16(5); got != 0 {
322		t.Errorf("0>>5 == %d, want 0", got)
323	}
324	if got := ofz64r8(5); got != 0 {
325		t.Errorf("0>>5 == %d, want 0", got)
326	}
327	if got := ofz64ur64(5); got != 0 {
328		t.Errorf("0>>>5 == %d, want 0", got)
329	}
330	if got := ofz64ur32(5); got != 0 {
331		t.Errorf("0>>>5 == %d, want 0", got)
332	}
333	if got := ofz64ur16(5); got != 0 {
334		t.Errorf("0>>>5 == %d, want 0", got)
335	}
336	if got := ofz64ur8(5); got != 0 {
337		t.Errorf("0>>>5 == %d, want 0", got)
338	}
339
340	if got := ofz32l64(5); got != 0 {
341		t.Errorf("0<<5 == %d, want 0", got)
342	}
343	if got := ofz32l32(5); got != 0 {
344		t.Errorf("0<<5 == %d, want 0", got)
345	}
346	if got := ofz32l16(5); got != 0 {
347		t.Errorf("0<<5 == %d, want 0", got)
348	}
349	if got := ofz32l8(5); got != 0 {
350		t.Errorf("0<<5 == %d, want 0", got)
351	}
352	if got := ofz32r64(5); got != 0 {
353		t.Errorf("0>>5 == %d, want 0", got)
354	}
355	if got := ofz32r32(5); got != 0 {
356		t.Errorf("0>>5 == %d, want 0", got)
357	}
358	if got := ofz32r16(5); got != 0 {
359		t.Errorf("0>>5 == %d, want 0", got)
360	}
361	if got := ofz32r8(5); got != 0 {
362		t.Errorf("0>>5 == %d, want 0", got)
363	}
364	if got := ofz32ur64(5); got != 0 {
365		t.Errorf("0>>>5 == %d, want 0", got)
366	}
367	if got := ofz32ur32(5); got != 0 {
368		t.Errorf("0>>>5 == %d, want 0", got)
369	}
370	if got := ofz32ur16(5); got != 0 {
371		t.Errorf("0>>>5 == %d, want 0", got)
372	}
373	if got := ofz32ur8(5); got != 0 {
374		t.Errorf("0>>>5 == %d, want 0", got)
375	}
376
377	if got := ofz16l64(5); got != 0 {
378		t.Errorf("0<<5 == %d, want 0", got)
379	}
380	if got := ofz16l32(5); got != 0 {
381		t.Errorf("0<<5 == %d, want 0", got)
382	}
383	if got := ofz16l16(5); got != 0 {
384		t.Errorf("0<<5 == %d, want 0", got)
385	}
386	if got := ofz16l8(5); got != 0 {
387		t.Errorf("0<<5 == %d, want 0", got)
388	}
389	if got := ofz16r64(5); got != 0 {
390		t.Errorf("0>>5 == %d, want 0", got)
391	}
392	if got := ofz16r32(5); got != 0 {
393		t.Errorf("0>>5 == %d, want 0", got)
394	}
395	if got := ofz16r16(5); got != 0 {
396		t.Errorf("0>>5 == %d, want 0", got)
397	}
398	if got := ofz16r8(5); got != 0 {
399		t.Errorf("0>>5 == %d, want 0", got)
400	}
401	if got := ofz16ur64(5); got != 0 {
402		t.Errorf("0>>>5 == %d, want 0", got)
403	}
404	if got := ofz16ur32(5); got != 0 {
405		t.Errorf("0>>>5 == %d, want 0", got)
406	}
407	if got := ofz16ur16(5); got != 0 {
408		t.Errorf("0>>>5 == %d, want 0", got)
409	}
410	if got := ofz16ur8(5); got != 0 {
411		t.Errorf("0>>>5 == %d, want 0", got)
412	}
413
414	if got := ofz8l64(5); got != 0 {
415		t.Errorf("0<<5 == %d, want 0", got)
416	}
417	if got := ofz8l32(5); got != 0 {
418		t.Errorf("0<<5 == %d, want 0", got)
419	}
420	if got := ofz8l16(5); got != 0 {
421		t.Errorf("0<<5 == %d, want 0", got)
422	}
423	if got := ofz8l8(5); got != 0 {
424		t.Errorf("0<<5 == %d, want 0", got)
425	}
426	if got := ofz8r64(5); got != 0 {
427		t.Errorf("0>>5 == %d, want 0", got)
428	}
429	if got := ofz8r32(5); got != 0 {
430		t.Errorf("0>>5 == %d, want 0", got)
431	}
432	if got := ofz8r16(5); got != 0 {
433		t.Errorf("0>>5 == %d, want 0", got)
434	}
435	if got := ofz8r8(5); got != 0 {
436		t.Errorf("0>>5 == %d, want 0", got)
437	}
438	if got := ofz8ur64(5); got != 0 {
439		t.Errorf("0>>>5 == %d, want 0", got)
440	}
441	if got := ofz8ur32(5); got != 0 {
442		t.Errorf("0>>>5 == %d, want 0", got)
443	}
444	if got := ofz8ur16(5); got != 0 {
445		t.Errorf("0>>>5 == %d, want 0", got)
446	}
447	if got := ofz8ur8(5); got != 0 {
448		t.Errorf("0>>>5 == %d, want 0", got)
449	}
450}
451
452//go:noinline
453func byz64l(n int64) int64 {
454	return n << 0
455}
456
457//go:noinline
458func byz64r(n int64) int64 {
459	return n >> 0
460}
461
462//go:noinline
463func byz64ur(n uint64) uint64 {
464	return n >> 0
465}
466
467//go:noinline
468func byz32l(n int32) int32 {
469	return n << 0
470}
471
472//go:noinline
473func byz32r(n int32) int32 {
474	return n >> 0
475}
476
477//go:noinline
478func byz32ur(n uint32) uint32 {
479	return n >> 0
480}
481
482//go:noinline
483func byz16l(n int16) int16 {
484	return n << 0
485}
486
487//go:noinline
488func byz16r(n int16) int16 {
489	return n >> 0
490}
491
492//go:noinline
493func byz16ur(n uint16) uint16 {
494	return n >> 0
495}
496
497//go:noinline
498func byz8l(n int8) int8 {
499	return n << 0
500}
501
502//go:noinline
503func byz8r(n int8) int8 {
504	return n >> 0
505}
506
507//go:noinline
508func byz8ur(n uint8) uint8 {
509	return n >> 0
510}
511
512func TestShiftByZero(t *testing.T) {
513	{
514		var n int64 = 0x5555555555555555
515		if got := byz64l(n); got != n {
516			t.Errorf("%x<<0 == %x, want %x", n, got, n)
517		}
518		if got := byz64r(n); got != n {
519			t.Errorf("%x>>0 == %x, want %x", n, got, n)
520		}
521	}
522	{
523		var n uint64 = 0xaaaaaaaaaaaaaaaa
524		if got := byz64ur(n); got != n {
525			t.Errorf("%x>>>0 == %x, want %x", n, got, n)
526		}
527	}
528
529	{
530		var n int32 = 0x55555555
531		if got := byz32l(n); got != n {
532			t.Errorf("%x<<0 == %x, want %x", n, got, n)
533		}
534		if got := byz32r(n); got != n {
535			t.Errorf("%x>>0 == %x, want %x", n, got, n)
536		}
537	}
538	{
539		var n uint32 = 0xaaaaaaaa
540		if got := byz32ur(n); got != n {
541			t.Errorf("%x>>>0 == %x, want %x", n, got, n)
542		}
543	}
544
545	{
546		var n int16 = 0x5555
547		if got := byz16l(n); got != n {
548			t.Errorf("%x<<0 == %x, want %x", n, got, n)
549		}
550		if got := byz16r(n); got != n {
551			t.Errorf("%x>>0 == %x, want %x", n, got, n)
552		}
553	}
554	{
555		var n uint16 = 0xaaaa
556		if got := byz16ur(n); got != n {
557			t.Errorf("%x>>>0 == %x, want %x", n, got, n)
558		}
559	}
560
561	{
562		var n int8 = 0x55
563		if got := byz8l(n); got != n {
564			t.Errorf("%x<<0 == %x, want %x", n, got, n)
565		}
566		if got := byz8r(n); got != n {
567			t.Errorf("%x>>0 == %x, want %x", n, got, n)
568		}
569	}
570	{
571		var n uint8 = 0x55
572		if got := byz8ur(n); got != n {
573			t.Errorf("%x>>>0 == %x, want %x", n, got, n)
574		}
575	}
576}
577
578//go:noinline
579func two64l(x int64) int64 {
580	return x << 1 << 1
581}
582
583//go:noinline
584func two64r(x int64) int64 {
585	return x >> 1 >> 1
586}
587
588//go:noinline
589func two64ur(x uint64) uint64 {
590	return x >> 1 >> 1
591}
592
593//go:noinline
594func two32l(x int32) int32 {
595	return x << 1 << 1
596}
597
598//go:noinline
599func two32r(x int32) int32 {
600	return x >> 1 >> 1
601}
602
603//go:noinline
604func two32ur(x uint32) uint32 {
605	return x >> 1 >> 1
606}
607
608//go:noinline
609func two16l(x int16) int16 {
610	return x << 1 << 1
611}
612
613//go:noinline
614func two16r(x int16) int16 {
615	return x >> 1 >> 1
616}
617
618//go:noinline
619func two16ur(x uint16) uint16 {
620	return x >> 1 >> 1
621}
622
623//go:noinline
624func two8l(x int8) int8 {
625	return x << 1 << 1
626}
627
628//go:noinline
629func two8r(x int8) int8 {
630	return x >> 1 >> 1
631}
632
633//go:noinline
634func two8ur(x uint8) uint8 {
635	return x >> 1 >> 1
636}
637
638func TestShiftCombine(t *testing.T) {
639	if got, want := two64l(4), int64(16); want != got {
640		t.Errorf("4<<1<<1 == %d, want %d", got, want)
641	}
642	if got, want := two64r(64), int64(16); want != got {
643		t.Errorf("64>>1>>1 == %d, want %d", got, want)
644	}
645	if got, want := two64ur(64), uint64(16); want != got {
646		t.Errorf("64>>1>>1 == %d, want %d", got, want)
647	}
648	if got, want := two32l(4), int32(16); want != got {
649		t.Errorf("4<<1<<1 == %d, want %d", got, want)
650	}
651	if got, want := two32r(64), int32(16); want != got {
652		t.Errorf("64>>1>>1 == %d, want %d", got, want)
653	}
654	if got, want := two32ur(64), uint32(16); want != got {
655		t.Errorf("64>>1>>1 == %d, want %d", got, want)
656	}
657	if got, want := two16l(4), int16(16); want != got {
658		t.Errorf("4<<1<<1 == %d, want %d", got, want)
659	}
660	if got, want := two16r(64), int16(16); want != got {
661		t.Errorf("64>>1>>1 == %d, want %d", got, want)
662	}
663	if got, want := two16ur(64), uint16(16); want != got {
664		t.Errorf("64>>1>>1 == %d, want %d", got, want)
665	}
666	if got, want := two8l(4), int8(16); want != got {
667		t.Errorf("4<<1<<1 == %d, want %d", got, want)
668	}
669	if got, want := two8r(64), int8(16); want != got {
670		t.Errorf("64>>1>>1 == %d, want %d", got, want)
671	}
672	if got, want := two8ur(64), uint8(16); want != got {
673		t.Errorf("64>>1>>1 == %d, want %d", got, want)
674	}
675
676}
677
678//go:noinline
679func three64l(x int64) int64 {
680	return x << 3 >> 1 << 2
681}
682
683//go:noinline
684func three64ul(x uint64) uint64 {
685	return x << 3 >> 1 << 2
686}
687
688//go:noinline
689func three64r(x int64) int64 {
690	return x >> 3 << 1 >> 2
691}
692
693//go:noinline
694func three64ur(x uint64) uint64 {
695	return x >> 3 << 1 >> 2
696}
697
698//go:noinline
699func three32l(x int32) int32 {
700	return x << 3 >> 1 << 2
701}
702
703//go:noinline
704func three32ul(x uint32) uint32 {
705	return x << 3 >> 1 << 2
706}
707
708//go:noinline
709func three32r(x int32) int32 {
710	return x >> 3 << 1 >> 2
711}
712
713//go:noinline
714func three32ur(x uint32) uint32 {
715	return x >> 3 << 1 >> 2
716}
717
718//go:noinline
719func three16l(x int16) int16 {
720	return x << 3 >> 1 << 2
721}
722
723//go:noinline
724func three16ul(x uint16) uint16 {
725	return x << 3 >> 1 << 2
726}
727
728//go:noinline
729func three16r(x int16) int16 {
730	return x >> 3 << 1 >> 2
731}
732
733//go:noinline
734func three16ur(x uint16) uint16 {
735	return x >> 3 << 1 >> 2
736}
737
738//go:noinline
739func three8l(x int8) int8 {
740	return x << 3 >> 1 << 2
741}
742
743//go:noinline
744func three8ul(x uint8) uint8 {
745	return x << 3 >> 1 << 2
746}
747
748//go:noinline
749func three8r(x int8) int8 {
750	return x >> 3 << 1 >> 2
751}
752
753//go:noinline
754func three8ur(x uint8) uint8 {
755	return x >> 3 << 1 >> 2
756}
757
758func TestShiftCombine3(t *testing.T) {
759	if got, want := three64l(4), int64(64); want != got {
760		t.Errorf("4<<1<<1 == %d, want %d", got, want)
761	}
762	if got, want := three64ul(4), uint64(64); want != got {
763		t.Errorf("4<<1<<1 == %d, want %d", got, want)
764	}
765	if got, want := three64r(64), int64(4); want != got {
766		t.Errorf("64>>1>>1 == %d, want %d", got, want)
767	}
768	if got, want := three64ur(64), uint64(4); want != got {
769		t.Errorf("64>>1>>1 == %d, want %d", got, want)
770	}
771	if got, want := three32l(4), int32(64); want != got {
772		t.Errorf("4<<1<<1 == %d, want %d", got, want)
773	}
774	if got, want := three32ul(4), uint32(64); want != got {
775		t.Errorf("4<<1<<1 == %d, want %d", got, want)
776	}
777	if got, want := three32r(64), int32(4); want != got {
778		t.Errorf("64>>1>>1 == %d, want %d", got, want)
779	}
780	if got, want := three32ur(64), uint32(4); want != got {
781		t.Errorf("64>>1>>1 == %d, want %d", got, want)
782	}
783	if got, want := three16l(4), int16(64); want != got {
784		t.Errorf("4<<1<<1 == %d, want %d", got, want)
785	}
786	if got, want := three16ul(4), uint16(64); want != got {
787		t.Errorf("4<<1<<1 == %d, want %d", got, want)
788	}
789	if got, want := three16r(64), int16(4); want != got {
790		t.Errorf("64>>1>>1 == %d, want %d", got, want)
791	}
792	if got, want := three16ur(64), uint16(4); want != got {
793		t.Errorf("64>>1>>1 == %d, want %d", got, want)
794	}
795	if got, want := three8l(4), int8(64); want != got {
796		t.Errorf("4<<1<<1 == %d, want %d", got, want)
797	}
798	if got, want := three8ul(4), uint8(64); want != got {
799		t.Errorf("4<<1<<1 == %d, want %d", got, want)
800	}
801	if got, want := three8r(64), int8(4); want != got {
802		t.Errorf("64>>1>>1 == %d, want %d", got, want)
803	}
804	if got, want := three8ur(64), uint8(4); want != got {
805		t.Errorf("64>>1>>1 == %d, want %d", got, want)
806	}
807}
808
809var (
810	one64  int64  = 1
811	one64u uint64 = 1
812	one32  int32  = 1
813	one32u uint32 = 1
814	one16  int16  = 1
815	one16u uint16 = 1
816	one8   int8   = 1
817	one8u  uint8  = 1
818)
819
820func TestShiftLargeCombine(t *testing.T) {
821	var N uint64 = 0x8000000000000000
822	if one64<<N<<N == 1 {
823		t.Errorf("shift overflow mishandled")
824	}
825	if one64>>N>>N == 1 {
826		t.Errorf("shift overflow mishandled")
827	}
828	if one64u>>N>>N == 1 {
829		t.Errorf("shift overflow mishandled")
830	}
831	if one32<<N<<N == 1 {
832		t.Errorf("shift overflow mishandled")
833	}
834	if one32>>N>>N == 1 {
835		t.Errorf("shift overflow mishandled")
836	}
837	if one32u>>N>>N == 1 {
838		t.Errorf("shift overflow mishandled")
839	}
840	if one16<<N<<N == 1 {
841		t.Errorf("shift overflow mishandled")
842	}
843	if one16>>N>>N == 1 {
844		t.Errorf("shift overflow mishandled")
845	}
846	if one16u>>N>>N == 1 {
847		t.Errorf("shift overflow mishandled")
848	}
849	if one8<<N<<N == 1 {
850		t.Errorf("shift overflow mishandled")
851	}
852	if one8>>N>>N == 1 {
853		t.Errorf("shift overflow mishandled")
854	}
855	if one8u>>N>>N == 1 {
856		t.Errorf("shift overflow mishandled")
857	}
858}
859
860func TestShiftLargeCombine3(t *testing.T) {
861	var N uint64 = 0x8000000000000001
862	if one64<<N>>2<<N == 1 {
863		t.Errorf("shift overflow mishandled")
864	}
865	if one64u<<N>>2<<N == 1 {
866		t.Errorf("shift overflow mishandled")
867	}
868	if one64>>N<<2>>N == 1 {
869		t.Errorf("shift overflow mishandled")
870	}
871	if one64u>>N<<2>>N == 1 {
872		t.Errorf("shift overflow mishandled")
873	}
874	if one32<<N>>2<<N == 1 {
875		t.Errorf("shift overflow mishandled")
876	}
877	if one32u<<N>>2<<N == 1 {
878		t.Errorf("shift overflow mishandled")
879	}
880	if one32>>N<<2>>N == 1 {
881		t.Errorf("shift overflow mishandled")
882	}
883	if one32u>>N<<2>>N == 1 {
884		t.Errorf("shift overflow mishandled")
885	}
886	if one16<<N>>2<<N == 1 {
887		t.Errorf("shift overflow mishandled")
888	}
889	if one16u<<N>>2<<N == 1 {
890		t.Errorf("shift overflow mishandled")
891	}
892	if one16>>N<<2>>N == 1 {
893		t.Errorf("shift overflow mishandled")
894	}
895	if one16u>>N<<2>>N == 1 {
896		t.Errorf("shift overflow mishandled")
897	}
898	if one8<<N>>2<<N == 1 {
899		t.Errorf("shift overflow mishandled")
900	}
901	if one8u<<N>>2<<N == 1 {
902		t.Errorf("shift overflow mishandled")
903	}
904	if one8>>N<<2>>N == 1 {
905		t.Errorf("shift overflow mishandled")
906	}
907	if one8u>>N<<2>>N == 1 {
908		t.Errorf("shift overflow mishandled")
909	}
910}
911
912func TestShiftGeneric(t *testing.T) {
913	for _, test := range [...]struct {
914		valueWidth int
915		signed     bool
916		shiftWidth int
917		left       bool
918		f          interface{}
919	}{
920		{64, true, 64, true, func(n int64, s uint64) int64 { return n << s }},
921		{64, true, 64, false, func(n int64, s uint64) int64 { return n >> s }},
922		{64, false, 64, false, func(n uint64, s uint64) uint64 { return n >> s }},
923		{64, true, 32, true, func(n int64, s uint32) int64 { return n << s }},
924		{64, true, 32, false, func(n int64, s uint32) int64 { return n >> s }},
925		{64, false, 32, false, func(n uint64, s uint32) uint64 { return n >> s }},
926		{64, true, 16, true, func(n int64, s uint16) int64 { return n << s }},
927		{64, true, 16, false, func(n int64, s uint16) int64 { return n >> s }},
928		{64, false, 16, false, func(n uint64, s uint16) uint64 { return n >> s }},
929		{64, true, 8, true, func(n int64, s uint8) int64 { return n << s }},
930		{64, true, 8, false, func(n int64, s uint8) int64 { return n >> s }},
931		{64, false, 8, false, func(n uint64, s uint8) uint64 { return n >> s }},
932
933		{32, true, 64, true, func(n int32, s uint64) int32 { return n << s }},
934		{32, true, 64, false, func(n int32, s uint64) int32 { return n >> s }},
935		{32, false, 64, false, func(n uint32, s uint64) uint32 { return n >> s }},
936		{32, true, 32, true, func(n int32, s uint32) int32 { return n << s }},
937		{32, true, 32, false, func(n int32, s uint32) int32 { return n >> s }},
938		{32, false, 32, false, func(n uint32, s uint32) uint32 { return n >> s }},
939		{32, true, 16, true, func(n int32, s uint16) int32 { return n << s }},
940		{32, true, 16, false, func(n int32, s uint16) int32 { return n >> s }},
941		{32, false, 16, false, func(n uint32, s uint16) uint32 { return n >> s }},
942		{32, true, 8, true, func(n int32, s uint8) int32 { return n << s }},
943		{32, true, 8, false, func(n int32, s uint8) int32 { return n >> s }},
944		{32, false, 8, false, func(n uint32, s uint8) uint32 { return n >> s }},
945
946		{16, true, 64, true, func(n int16, s uint64) int16 { return n << s }},
947		{16, true, 64, false, func(n int16, s uint64) int16 { return n >> s }},
948		{16, false, 64, false, func(n uint16, s uint64) uint16 { return n >> s }},
949		{16, true, 32, true, func(n int16, s uint32) int16 { return n << s }},
950		{16, true, 32, false, func(n int16, s uint32) int16 { return n >> s }},
951		{16, false, 32, false, func(n uint16, s uint32) uint16 { return n >> s }},
952		{16, true, 16, true, func(n int16, s uint16) int16 { return n << s }},
953		{16, true, 16, false, func(n int16, s uint16) int16 { return n >> s }},
954		{16, false, 16, false, func(n uint16, s uint16) uint16 { return n >> s }},
955		{16, true, 8, true, func(n int16, s uint8) int16 { return n << s }},
956		{16, true, 8, false, func(n int16, s uint8) int16 { return n >> s }},
957		{16, false, 8, false, func(n uint16, s uint8) uint16 { return n >> s }},
958
959		{8, true, 64, true, func(n int8, s uint64) int8 { return n << s }},
960		{8, true, 64, false, func(n int8, s uint64) int8 { return n >> s }},
961		{8, false, 64, false, func(n uint8, s uint64) uint8 { return n >> s }},
962		{8, true, 32, true, func(n int8, s uint32) int8 { return n << s }},
963		{8, true, 32, false, func(n int8, s uint32) int8 { return n >> s }},
964		{8, false, 32, false, func(n uint8, s uint32) uint8 { return n >> s }},
965		{8, true, 16, true, func(n int8, s uint16) int8 { return n << s }},
966		{8, true, 16, false, func(n int8, s uint16) int8 { return n >> s }},
967		{8, false, 16, false, func(n uint8, s uint16) uint8 { return n >> s }},
968		{8, true, 8, true, func(n int8, s uint8) int8 { return n << s }},
969		{8, true, 8, false, func(n int8, s uint8) int8 { return n >> s }},
970		{8, false, 8, false, func(n uint8, s uint8) uint8 { return n >> s }},
971	} {
972		fv := reflect.ValueOf(test.f)
973		var args [2]reflect.Value
974		for i := 0; i < test.valueWidth; i++ {
975			// Build value to be shifted.
976			var n int64 = 1
977			for j := 0; j < i; j++ {
978				n <<= 1
979			}
980			args[0] = reflect.ValueOf(n).Convert(fv.Type().In(0))
981			for s := 0; s <= test.shiftWidth; s++ {
982				args[1] = reflect.ValueOf(s).Convert(fv.Type().In(1))
983
984				// Compute desired result. We're testing variable shifts
985				// assuming constant shifts are correct.
986				r := n
987				var op string
988				switch {
989				case test.left:
990					op = "<<"
991					for j := 0; j < s; j++ {
992						r <<= 1
993					}
994					switch test.valueWidth {
995					case 32:
996						r = int64(int32(r))
997					case 16:
998						r = int64(int16(r))
999					case 8:
1000						r = int64(int8(r))
1001					}
1002				case test.signed:
1003					op = ">>"
1004					switch test.valueWidth {
1005					case 32:
1006						r = int64(int32(r))
1007					case 16:
1008						r = int64(int16(r))
1009					case 8:
1010						r = int64(int8(r))
1011					}
1012					for j := 0; j < s; j++ {
1013						r >>= 1
1014					}
1015				default:
1016					op = ">>>"
1017					for j := 0; j < s; j++ {
1018						r = int64(uint64(r) >> 1)
1019					}
1020				}
1021
1022				// Call function.
1023				res := fv.Call(args[:])[0].Convert(reflect.ValueOf(r).Type())
1024
1025				if res.Int() != r {
1026					t.Errorf("%s%dx%d(%x,%x)=%x, want %x", op, test.valueWidth, test.shiftWidth, n, s, res.Int(), r)
1027				}
1028			}
1029		}
1030	}
1031}
1032
Full Screen

util.go

Source: util.go Github

copy
1// Copyright 2020 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package tests
6
7import (
8	"bytes"
9	"context"
10	"fmt"
11	"go/token"
12	"path/filepath"
13	"sort"
14	"strconv"
15	"strings"
16	"testing"
17
18	"golang.org/x/tools/internal/lsp/diff"
19	"golang.org/x/tools/internal/lsp/diff/myers"
20	"golang.org/x/tools/internal/lsp/protocol"
21	"golang.org/x/tools/internal/lsp/source"
22	"golang.org/x/tools/internal/lsp/source/completion"
23	"golang.org/x/tools/internal/span"
24)
25
26// DiffLinks takes the links we got and checks if they are located within the source or a Note.
27// If the link is within a Note, the link is removed.
28// Returns an diff comment if there are differences and empty string if no diffs.
29func DiffLinks(mapper *protocol.ColumnMapper, wantLinks []Link, gotLinks []protocol.DocumentLink) string {
30	var notePositions []token.Position
31	links := make(map[span.Span]string, len(wantLinks))
32	for _, link := range wantLinks {
33		links[link.Src] = link.Target
34		notePositions = append(notePositions, link.NotePosition)
35	}
36	for _, link := range gotLinks {
37		spn, err := mapper.RangeSpan(link.Range)
38		if err != nil {
39			return fmt.Sprintf("%v", err)
40		}
41		linkInNote := false
42		for _, notePosition := range notePositions {
43			// Drop the links found inside expectation notes arguments as this links are not collected by expect package.
44			if notePosition.Line == spn.Start().Line() &&
45				notePosition.Column <= spn.Start().Column() {
46				delete(links, spn)
47				linkInNote = true
48			}
49		}
50		if linkInNote {
51			continue
52		}
53		if target, ok := links[spn]; ok {
54			delete(links, spn)
55			if target != link.Target {
56				return fmt.Sprintf("for %v want %v, got %v\n", spn, target, link.Target)
57			}
58		} else {
59			return fmt.Sprintf("unexpected link %v:%v\n", spn, link.Target)
60		}
61	}
62	for spn, target := range links {
63		return fmt.Sprintf("missing link %v:%v\n", spn, target)
64	}
65	return ""
66}
67
68// DiffSymbols prints the diff between expected and actual symbols test results.
69func DiffSymbols(t *testing.T, uri span.URI, want, got []protocol.DocumentSymbol) string {
70	sort.Slice(want, func(i, j int) bool { return want[i].Name < want[j].Name })
71	sort.Slice(got, func(i, j int) bool { return got[i].Name < got[j].Name })
72	if len(got) != len(want) {
73		return summarizeSymbols(-1, want, got, "different lengths got %v want %v", len(got), len(want))
74	}
75	for i, w := range want {
76		g := got[i]
77		if w.Name != g.Name {
78			return summarizeSymbols(i, want, got, "incorrect name got %v want %v", g.Name, w.Name)
79		}
80		if w.Kind != g.Kind {
81			return summarizeSymbols(i, want, got, "incorrect kind got %v want %v", g.Kind, w.Kind)
82		}
83		if protocol.CompareRange(w.SelectionRange, g.SelectionRange) != 0 {
84			return summarizeSymbols(i, want, got, "incorrect span got %v want %v", g.SelectionRange, w.SelectionRange)
85		}
86		if msg := DiffSymbols(t, uri, w.Children, g.Children); msg != "" {
87			return fmt.Sprintf("children of %s: %s", w.Name, msg)
88		}
89	}
90	return ""
91}
92
93func summarizeSymbols(i int, want, got []protocol.DocumentSymbol, reason string, args ...interface{}) string {
94	msg := &bytes.Buffer{}
95	fmt.Fprint(msg, "document symbols failed")
96	if i >= 0 {
97		fmt.Fprintf(msg, " at %d", i)
98	}
99	fmt.Fprint(msg, " because of ")
100	fmt.Fprintf(msg, reason, args...)
101	fmt.Fprint(msg, ":\nexpected:\n")
102	for _, s := range want {
103		fmt.Fprintf(msg, "  %v %v %v\n", s.Name, s.Kind, s.SelectionRange)
104	}
105	fmt.Fprintf(msg, "got:\n")
106	for _, s := range got {
107		fmt.Fprintf(msg, "  %v %v %v\n", s.Name, s.Kind, s.SelectionRange)
108	}
109	return msg.String()
110}
111
112// DiffDiagnostics prints the diff between expected and actual diagnostics test
113// results.
114func DiffDiagnostics(uri span.URI, want, got []*source.Diagnostic) string {
115	source.SortDiagnostics(want)
116	source.SortDiagnostics(got)
117
118	if len(got) != len(want) {
119		return summarizeDiagnostics(-1, uri, want, got, "different lengths got %v want %v", len(got), len(want))
120	}
121	for i, w := range want {
122		g := got[i]
123		if w.Message != g.Message {
124			return summarizeDiagnostics(i, uri, want, got, "incorrect Message got %v want %v", g.Message, w.Message)
125		}
126		if w.Severity != g.Severity {
127			return summarizeDiagnostics(i, uri, want, got, "incorrect Severity got %v want %v", g.Severity, w.Severity)
128		}
129		if w.Source != g.Source {
130			return summarizeDiagnostics(i, uri, want, got, "incorrect Source got %v want %v", g.Source, w.Source)
131		}
132		if !rangeOverlaps(g.Range, w.Range) {
133			return summarizeDiagnostics(i, uri, want, got, "range %v does not overlap %v", g.Range, w.Range)
134		}
135	}
136	return ""
137}
138
139// rangeOverlaps reports whether r1 and r2 overlap.
140func rangeOverlaps(r1, r2 protocol.Range) bool {
141	if inRange(r2.Start, r1) || inRange(r1.Start, r2) {
142		return true
143	}
144	return false
145}
146
147// inRange reports whether p is contained within [r.Start, r.End), or if p ==
148// r.Start == r.End (special handling for the case where the range is a single
149// point).
150func inRange(p protocol.Position, r protocol.Range) bool {
151	if protocol.IsPoint(r) {
152		return protocol.ComparePosition(r.Start, p) == 0
153	}
154	if protocol.ComparePosition(r.Start, p) <= 0 && protocol.ComparePosition(p, r.End) < 0 {
155		return true
156	}
157	return false
158}
159
160func summarizeDiagnostics(i int, uri span.URI, want, got []*source.Diagnostic, reason string, args ...interface{}) string {
161	msg := &bytes.Buffer{}
162	fmt.Fprint(msg, "diagnostics failed")
163	if i >= 0 {
164		fmt.Fprintf(msg, " at %d", i)
165	}
166	fmt.Fprint(msg, " because of ")
167	fmt.Fprintf(msg, reason, args...)
168	fmt.Fprint(msg, ":\nexpected:\n")
169	for _, d := range want {
170		fmt.Fprintf(msg, "  %s:%v: %s\n", uri, d.Range, d.Message)
171	}
172	fmt.Fprintf(msg, "got:\n")
173	for _, d := range got {
174		fmt.Fprintf(msg, "  %s:%v: %s\n", uri, d.Range, d.Message)
175	}
176	return msg.String()
177}
178
179func DiffCodeLens(uri span.URI, want, got []protocol.CodeLens) string {
180	sortCodeLens(want)
181	sortCodeLens(got)
182
183	if len(got) != len(want) {
184		return summarizeCodeLens(-1, uri, want, got, "different lengths got %v want %v", len(got), len(want))
185	}
186	for i, w := range want {
187		g := got[i]
188		if w.Command.Command != g.Command.Command {
189			return summarizeCodeLens(i, uri, want, got, "incorrect Command Name got %v want %v", g.Command.Command, w.Command.Command)
190		}
191		if w.Command.Title != g.Command.Title {
192			return summarizeCodeLens(i, uri, want, got, "incorrect Command Title got %v want %v", g.Command.Title, w.Command.Title)
193		}
194		if protocol.ComparePosition(w.Range.Start, g.Range.Start) != 0 {
195			return summarizeCodeLens(i, uri, want, got, "incorrect Start got %v want %v", g.Range.Start, w.Range.Start)
196		}
197		if !protocol.IsPoint(g.Range) { // Accept any 'want' range if the codelens returns a zero-length range.
198			if protocol.ComparePosition(w.Range.End, g.Range.End) != 0 {
199				return summarizeCodeLens(i, uri, want, got, "incorrect End got %v want %v", g.Range.End, w.Range.End)
200			}
201		}
202	}
203	return ""
204}
205
206func sortCodeLens(c []protocol.CodeLens) {
207	sort.Slice(c, func(i int, j int) bool {
208		if r := protocol.CompareRange(c[i].Range, c[j].Range); r != 0 {
209			return r < 0
210		}
211		if c[i].Command.Command < c[j].Command.Command {
212			return true
213		} else if c[i].Command.Command == c[j].Command.Command {
214			return c[i].Command.Title < c[j].Command.Title
215		} else {
216			return false
217		}
218	})
219}
220
221func summarizeCodeLens(i int, uri span.URI, want, got []protocol.CodeLens, reason string, args ...interface{}) string {
222	msg := &bytes.Buffer{}
223	fmt.Fprint(msg, "codelens failed")
224	if i >= 0 {
225		fmt.Fprintf(msg, " at %d", i)
226	}
227	fmt.Fprint(msg, " because of ")
228	fmt.Fprintf(msg, reason, args...)
229	fmt.Fprint(msg, ":\nexpected:\n")
230	for _, d := range want {
231		fmt.Fprintf(msg, "  %s:%v: %s | %s\n", uri, d.Range, d.Command.Command, d.Command.Title)
232	}
233	fmt.Fprintf(msg, "got:\n")
234	for _, d := range got {
235		fmt.Fprintf(msg, "  %s:%v: %s | %s\n", uri, d.Range, d.Command.Command, d.Command.Title)
236	}
237	return msg.String()
238}
239
240func DiffSignatures(spn span.Span, want, got *protocol.SignatureHelp) (string, error) {
241	decorate := func(f string, args ...interface{}) string {
242		return fmt.Sprintf("invalid signature at %s: %s", spn, fmt.Sprintf(f, args...))
243	}
244	if len(got.Signatures) != 1 {
245		return decorate("wanted 1 signature, got %d", len(got.Signatures)), nil
246	}
247	if got.ActiveSignature != 0 {
248		return decorate("wanted active signature of 0, got %d", int(got.ActiveSignature)), nil
249	}
250	if want.ActiveParameter != got.ActiveParameter {
251		return decorate("wanted active parameter of %d, got %d", want.ActiveParameter, int(got.ActiveParameter)), nil
252	}
253	g := got.Signatures[0]
254	w := want.Signatures[0]
255	if w.Label != g.Label {
256		wLabel := w.Label + "\n"
257		d, err := myers.ComputeEdits("", wLabel, g.Label+"\n")
258		if err != nil {
259			return "", err
260		}
261		return decorate("mismatched labels:\n%q", diff.ToUnified("want", "got", wLabel, d)), err
262	}
263	var paramParts []string
264	for _, p := range g.Parameters {
265		paramParts = append(paramParts, p.Label)
266	}
267	paramsStr := strings.Join(paramParts, ", ")
268	if !strings.Contains(g.Label, paramsStr) {
269		return decorate("expected signature %q to contain params %q", g.Label, paramsStr), nil
270	}
271	return "", nil
272}
273
274// DiffCallHierarchyItems returns the diff between expected and actual call locations for incoming/outgoing call hierarchies
275func DiffCallHierarchyItems(gotCalls []protocol.CallHierarchyItem, expectedCalls []protocol.CallHierarchyItem) string {
276	expected := make(map[protocol.Location]bool)
277	for _, call := range expectedCalls {
278		expected[protocol.Location{URI: call.URI, Range: call.Range}] = true
279	}
280
281	got := make(map[protocol.Location]bool)
282	for _, call := range gotCalls {
283		got[protocol.Location{URI: call.URI, Range: call.Range}] = true
284	}
285	if len(got) != len(expected) {
286		return fmt.Sprintf("expected %d calls but got %d", len(expected), len(got))
287	}
288	for spn := range got {
289		if !expected[spn] {
290			return fmt.Sprintf("incorrect calls, expected locations %v but got locations %v", expected, got)
291		}
292	}
293	return ""
294}
295
296func ToProtocolCompletionItems(items []completion.CompletionItem) []protocol.CompletionItem {
297	var result []protocol.CompletionItem
298	for _, item := range items {
299		result = append(result, ToProtocolCompletionItem(item))
300	}
301	return result
302}
303
304func ToProtocolCompletionItem(item completion.CompletionItem) protocol.CompletionItem {
305	pItem := protocol.CompletionItem{
306		Label:         item.Label,
307		Kind:          item.Kind,
308		Detail:        item.Detail,
309		Documentation: item.Documentation,
310		InsertText:    item.InsertText,
311		TextEdit: &protocol.TextEdit{
312			NewText: item.Snippet(),
313		},
314		// Negate score so best score has lowest sort text like real API.
315		SortText: fmt.Sprint(-item.Score),
316	}
317	if pItem.InsertText == "" {
318		pItem.InsertText = pItem.Label
319	}
320	return pItem
321}
322
323func FilterBuiltins(src span.Span, items []protocol.CompletionItem) []protocol.CompletionItem {
324	var (
325		got          []protocol.CompletionItem
326		wantBuiltins = strings.Contains(string(src.URI()), "builtins")
327		wantKeywords = strings.Contains(string(src.URI()), "keywords")
328	)
329	for _, item := range items {
330		if !wantBuiltins && isBuiltin(item.Label, item.Detail, item.Kind) {
331			continue
332		}
333
334		if !wantKeywords && token.Lookup(item.Label).IsKeyword() {
335			continue
336		}
337
338		got = append(got, item)
339	}
340	return got
341}
342
343func isBuiltin(label, detail string, kind protocol.CompletionItemKind) bool {
344	if detail == "" && kind == protocol.ClassCompletion {
345		return true
346	}
347	// Remaining builtin constants, variables, interfaces, and functions.
348	trimmed := label
349	if i := strings.Index(trimmed, "("); i >= 0 {
350		trimmed = trimmed[:i]
351	}
352	switch trimmed {
353	case "append", "cap", "close", "complex", "copy", "delete",
354		"error", "false", "imag", "iota", "len", "make", "new",
355		"nil", "panic", "print", "println", "real", "recover", "true":
356		return true
357	}
358	return false
359}
360
361func CheckCompletionOrder(want, got []protocol.CompletionItem, strictScores bool) string {
362	var (
363		matchedIdxs []int
364		lastGotIdx  int
365		lastGotSort float64
366		inOrder     = true
367		errorMsg    = "completions out of order"
368	)
369	for _, w := range want {
370		var found bool
371		for i, g := range got {
372			if w.Label == g.Label && w.Detail == g.Detail && w.Kind == g.Kind {
373				matchedIdxs = append(matchedIdxs, i)
374				found = true
375
376				if i < lastGotIdx {
377					inOrder = false
378				}
379				lastGotIdx = i
380
381				sort, _ := strconv.ParseFloat(g.SortText, 64)
382				if strictScores && len(matchedIdxs) > 1 && sort <= lastGotSort {
383					inOrder = false
384					errorMsg = "candidate scores not strictly decreasing"
385				}
386				lastGotSort = sort
387
388				break
389			}
390		}
391		if !found {
392			return summarizeCompletionItems(-1, []protocol.CompletionItem{w}, got, "didn't find expected completion")
393		}
394	}
395
396	sort.Ints(matchedIdxs)
397	matched := make([]protocol.CompletionItem, 0, len(matchedIdxs))
398	for _, idx := range matchedIdxs {
399		matched = append(matched, got[idx])
400	}
401
402	if !inOrder {
403		return summarizeCompletionItems(-1, want, matched, errorMsg)
404	}
405
406	return ""
407}
408
409func DiffSnippets(want string, got *protocol.CompletionItem) string {
410	if want == "" {
411		if got != nil {
412			x := got.TextEdit
413			return fmt.Sprintf("expected no snippet but got %s", x.NewText)
414		}
415	} else {
416		if got == nil {
417			return fmt.Sprintf("couldn't find completion matching %q", want)
418		}
419		x := got.TextEdit
420		if want != x.NewText {
421			return fmt.Sprintf("expected snippet %q, got %q", want, x.NewText)
422		}
423	}
424	return ""
425}
426
427func FindItem(list []protocol.CompletionItem, want completion.CompletionItem) *protocol.CompletionItem {
428	for _, item := range list {
429		if item.Label == want.Label {
430			return &item
431		}
432	}
433	return nil
434}
435
436// DiffCompletionItems prints the diff between expected and actual completion
437// test results.
438func DiffCompletionItems(want, got []protocol.CompletionItem) string {
439	if len(got) != len(want) {
440		return summarizeCompletionItems(-1, want, got, "different lengths got %v want %v", len(got), len(want))
441	}
442	for i, w := range want {
443		g := got[i]
444		if w.Label != g.Label {
445			return summarizeCompletionItems(i, want, got, "incorrect Label got %v want %v", g.Label, w.Label)
446		}
447		if w.Detail != g.Detail {
448			return summarizeCompletionItems(i, want, got, "incorrect Detail got %v want %v", g.Detail, w.Detail)
449		}
450		if w.Documentation != "" && !strings.HasPrefix(w.Documentation, "@") {
451			if w.Documentation != g.Documentation {
452				return summarizeCompletionItems(i, want, got, "incorrect Documentation got %v want %v", g.Documentation, w.Documentation)
453			}
454		}
455		if w.Kind != g.Kind {
456			return summarizeCompletionItems(i, want, got, "incorrect Kind got %v want %v", g.Kind, w.Kind)
457		}
458	}
459	return ""
460}
461
462func summarizeCompletionItems(i int, want, got []protocol.CompletionItem, reason string, args ...interface{}) string {
463	msg := &bytes.Buffer{}
464	fmt.Fprint(msg, "completion failed")
465	if i >= 0 {
466		fmt.Fprintf(msg, " at %d", i)
467	}
468	fmt.Fprint(msg, " because of ")
469	fmt.Fprintf(msg, reason, args...)
470	fmt.Fprint(msg, ":\nexpected:\n")
471	for _, d := range want {
472		fmt.Fprintf(msg, "  %v\n", d)
473	}
474	fmt.Fprintf(msg, "got:\n")
475	for _, d := range got {
476		fmt.Fprintf(msg, "  %v\n", d)
477	}
478	return msg.String()
479}
480
481func EnableAllAnalyzers(view source.View, opts *source.Options) {
482	if opts.Analyses == nil {
483		opts.Analyses = make(map[string]bool)
484	}
485	for _, a := range opts.DefaultAnalyzers {
486		if !a.IsEnabled(view) {
487			opts.Analyses[a.Analyzer.Name] = true
488		}
489	}
490	for _, a := range opts.TypeErrorAnalyzers {
491		if !a.IsEnabled(view) {
492			opts.Analyses[a.Analyzer.Name] = true
493		}
494	}
495	for _, a := range opts.ConvenienceAnalyzers {
496		if !a.IsEnabled(view) {
497			opts.Analyses[a.Analyzer.Name] = true
498		}
499	}
500	for _, a := range opts.StaticcheckAnalyzers {
501		if !a.IsEnabled(view) {
502			opts.Analyses[a.Analyzer.Name] = true
503		}
504	}
505}
506
507func WorkspaceSymbolsString(ctx context.Context, data *Data, queryURI span.URI, symbols []protocol.SymbolInformation) (string, error) {
508	queryDir := filepath.Dir(queryURI.Filename())
509	var filtered []string
510	for _, s := range symbols {
511		uri := s.Location.URI.SpanURI()
512		dir := filepath.Dir(uri.Filename())
513		if !source.InDir(queryDir, dir) { // assume queries always issue from higher directories
514			continue
515		}
516		m, err := data.Mapper(uri)
517		if err != nil {
518			return "", err
519		}
520		spn, err := m.Span(s.Location)
521		if err != nil {
522			return "", err
523		}
524		filtered = append(filtered, fmt.Sprintf("%s %s %s", spn, s.Name, s.Kind))
525	}
526	sort.Strings(filtered)
527	return strings.Join(filtered, "\n") + "\n", nil
528}
529
530func WorkspaceSymbolsTestTypeToMatcher(typ WorkspaceSymbolsTestType) source.SymbolMatcher {
531	switch typ {
532	case WorkspaceSymbolsFuzzy:
533		return source.SymbolFuzzy
534	case WorkspaceSymbolsCaseSensitive:
535		return source.SymbolCaseSensitive
536	default:
537		return source.SymbolCaseInsensitive
538	}
539}
540
541func Diff(t *testing.T, want, got string) string {
542	if want == got {
543		return ""
544	}
545	// Add newlines to avoid newline messages in diff.
546	want += "\n"
547	got += "\n"
548	d, err := myers.ComputeEdits("", want, got)
549	if err != nil {
550		t.Fatal(err)
551	}
552	return fmt.Sprintf("%q", diff.ToUnified("want", "got", want, d))
553}
554
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)