-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathstrings.rs
798 lines (711 loc) · 20.7 KB
/
strings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] test_pass_is_ascii verus_code! {
#[allow(unused_imports)]
use vstd::string::*;
fn str_is_ascii_passes() {
let x = ("Hello World");
proof {
reveal_strlit("Hello World");
}
assert(x.is_ascii());
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_fails_is_ascii verus_code! {
use vstd::string::*;
fn str_is_ascii_fails() {
let x = ("à");
proof {
reveal_strlit("à");
}
assert(x.is_ascii()); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_pass_get_char verus_code! {
use vstd::string::*;
fn get_char() {
let x = ("hello world");
proof {
reveal_strlit("hello world");
}
assert([email protected]() == 11);
let val = x.get_char(0);
assert('h' === val);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_fail_get_char verus_code! {
use vstd::string::*;
fn get_char_fails() {
let x = ("hello world");
let val = x.get_char(0); // FAILS
assert(val === 'h'); // FAILS
}
} => Err(err) => assert_fails(err, 2)
}
test_verify_one_file! {
#[test] test_passes_len verus_code! {
use vstd::string::*;
pub fn len_passes() {
let x = ("abcdef");
proof {
reveal_strlit("abcdef");
}
assert([email protected]() === 6);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_fails_len verus_code! {
use vstd::string::*;
pub fn len_fails() {
let x = ("abcdef");
proof {
reveal_strlit("abcdef");
}
assert([email protected]() == 1); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_passes_substring verus_code! {
use vstd::string::*;
fn test_substring_passes<'a>() -> (ret: &'a str)
ensures
[email protected](0,5) =~= ("Hello")@
{
proof {
reveal_strlit("Hello");
reveal_strlit("Hello World");
}
("Hello World")
}
fn test_substring_passes2<'a>() -> (ret: &'a str)
ensures
[email protected](0,5) =~= ("Hello")@
{
let x = ("Hello World");
proof {
reveal_strlit("Hello");
reveal_strlit("Hello World");
}
x.substring_ascii(0,5)
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_fails_substring verus_code! {
use vstd::string::*;
fn test_substring_fails<'a>() -> (ret: &'a str)
ensures
[email protected](0,5) =~= ("Hello")@ // FAILS
{
proof {
reveal_strlit("Hello");
reveal_strlit("Gello World");
}
("Gello World")
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_passes_multi verus_code! {
use vstd::string::*;
fn test_multi_passes() {
let a = ("a");
let a_clone = ("a");
let b = ("b");
let c = ("c");
let abc = ("abc");
let cba = ("cba");
let abc_clone = ("abc");
proof {
reveal_strlit("a");
reveal_strlit("b");
reveal_strlit("c");
reveal_strlit("d");
reveal_strlit("abc");
reveal_strlit("cba");
}
let a0 = a.get_char(0);
let a0_clone = a_clone.get_char(0);
let b0 = a.get_char(0);
let c0 = a.get_char(0);
assert(a !== b);
assert(b !== c);
assert(a === a);
assert(a0_clone === a0);
assert(a@ =~= [email protected](0,1));
assert(b@ =~= [email protected](1,2));
assert(c@ =~= [email protected](2,3));
assert(cba !== abc);
assert(abc === abc_clone);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_fails_multi verus_code! {
use vstd::string::*;
const x: &'static str = "Hello World";
const y: &'static str = "Gello World";
const z: &'static str = "Insert string here";
fn test_multi_fails1() {
assert([email protected]() === 11); // FAILS
}
fn test_multi_fails2() {
assert([email protected]() !== 11) // FAILS
}
fn test_multi_fails3() {
assert(x === y); // FAILS
}
} => Err(err) => assert_fails(err, 3)
}
test_verify_one_file! {
#[test] test_reveal_strlit_invalid_1 verus_code! {
use vstd::string::*;
fn test() {
proof {
reveal_strlit(12u32);
}
}
} => Err(err) => assert_vir_error_msg(err, "string literal expected")
}
test_verify_one_file! {
#[test] test_reveal_strlit_invalid_2 verus_code! {
use vstd::string::*;
fn test() {
proof {
reveal_strlit("a", "a");
}
}
} => Err(err) => assert_rust_error_msg(err, "this function takes 1 argument but 2 arguments were supplied")
}
test_verify_one_file! {
#[test] test_string_1_pass verus_code! {
use vstd::string::*;
fn test() {
let a = String::from_str(("A"));
proof {
reveal_strlit("A");
}
assert(a@ === ("A")@);
assert(a.is_ascii());
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_string_1_fail verus_code! {
use vstd::string::*;
fn test() {
let a = String::from_str(("A"));
proof {
reveal_strlit("A");
}
assert(a@ === ("B")@); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_strlit_neq verus_code! {
use vstd::string::*;
const x: &'static str = "Hello World";
const y: &'static str = "Gello World";
fn test() {
assert(x !== y);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_strlit_neq_soundness verus_code! {
use vstd::string::*;
const x: &'static str = "Hello World";
const y: &'static str = "Gello World";
fn test() {
assert(x !== y);
assert(false); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_char_passes verus_code! {
fn test_char_passes() {
let c = 'c';
assert(c == 'c');
}
fn test_char_passes1() {
let c = 'c';
assert(c != 'b');
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_char_fails verus_code! {
fn test_char_fails() {
let c = 'c';
assert(c == 'a'); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_char_unicode_passes verus_code! {
fn test_char_unicode_passes() {
let a = '💩';
assert(a == '💩');
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_len_return_passes verus_code! {
use vstd::string::*;
fn test_len_return_passes<'a>() -> (ret: usize)
ensures
ret == 4
{
proof {
reveal_strlit("abcd");
}
("abcd").unicode_len()
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_get_unicode_passes verus_code! {
use vstd::string::*;
fn test_get_unicode_passes() {
let x = ("Hello");
proof {
reveal_strlit("Hello");
}
let x0: char = x.get_char(0);
assert(x0 == 'H');
}
fn test_get_unicode_non_ascii_passes() {
let emoji_with_str = ("💩");
proof {
reveal_strlit("💩");
}
let p = emoji_with_str.get_char(0);
assert(p == '💩');
}
fn test_get_unicode_non_ascii_passes1() {
let emoji_with_str = ("abcdef💩");
proof {
reveal_strlit("abcdef💩");
}
let p = emoji_with_str.get_char(0);
assert(p == 'a');
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_unicode_substring_passes verus_code! {
use vstd::string::*;
fn test_substring_passes() {
proof {
reveal_strlit("01234💩");
reveal_strlit("012");
reveal_strlit("34💩");
}
let x = ("01234💩");
assert([email protected]() == 6);
let x0 = x.substring_char(0,3);
assert(x0@ =~= ("012")@);
let x1 = x.substring_char(3,6);
assert(x1@ =~= ("34💩")@);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_unicode_mixed_chars verus_code! {
use vstd::string::*;
proof fn test() {
let a = ("è ❤️");
reveal_strlit("è ❤️");
assert(a@[0] === 'è');
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_string_2_pass verus_code! {
use vstd::string::*;
fn test() {
let a = String::from_str(("ABC"));
proof {
reveal_strlit("ABC");
}
let b = a.as_str().substring_ascii(1, 2);
proof {
reveal_strlit("B");
}
assert(b@ =~= ("B")@);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_string_2_fail verus_code! {
use vstd::string::*;
fn test() {
let a = String::from_str(("ABC"));
proof {
reveal_strlit("ABC");
}
let b = a.as_str().substring_ascii(2, 3);
proof {
reveal_strlit("B");
reveal_strlit("C");
}
assert(b@ =~= ("C")@);
assert(b@ === ("B")@); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_string_is_ascii_roundtrip verus_code! {
use vstd::string::*;
fn test() {
let a = ("ABC");
let b = a.to_string();
let c = b.as_str();
proof {
reveal_strlit("ABC");
}
assert(a@ =~= c@);
assert(a.is_ascii());
assert(b.is_ascii());
assert(c.is_ascii());
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_ascii_handling_passes verus_code! {
use vstd::string::*;
fn test_get_ascii_passes() {
proof {
reveal_strlit("Hello World");
}
let x = ("Hello World");
let x0 = x.get_ascii(0);
assert(x0 === 72);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_ascii_ascii_handling_fails verus_code! {
use vstd::string::*;
fn test_get_ascii_fails() {
proof {
reveal_strlit("Hèllo World");
}
let y = ("Hèllo World");
let y0 = y.get_ascii(0); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_char_conversion_passes verus_code! {
use vstd::string::*;
fn test_char_conversion_passes() {
let c = 'c';
let d = c as u8;
// ascii value
assert(d === 99);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_char_conversion_fails verus_code! {
use vstd::string::*;
fn test_char_conversion_fails() {
let z = 'ž';
let d = z as u8;
assert(d == 382); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_char_conversion_u32 verus_code! {
use vstd::string::*;
fn test() {
let z = 'ž';
let d = z as u32;
assert(d == 382);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_strslice_get verus_code! {
use vstd::string::*;
fn test_strslice_get_passes<'a>(x: &'a str) -> (ret: u8)
requires
x.is_ascii(),
[email protected]() > 10
{
let x0 = x.get_char(0);
x0 as u8
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_strslice_as_bytes_passes verus_code! {
use vstd::view::*;
use vstd::string::*;
use vstd::prelude::*;
fn test_strslice_as_bytes<'a>(x: &'a str) -> (ret: Vec<u8>)
requires
x.is_ascii(),
[email protected]() > 10
ensures
[email protected]() > 10
{
x.as_bytes_vec()
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_strslice_as_bytes_fails verus_code! {
use vstd::view::*;
use vstd::string::*;
use vstd::prelude::*;
fn test_strslice_as_bytes_fails<'a>(x: &'a str) -> (ret: Vec<u8>)
requires
[email protected]() > 10
ensures
[email protected]() > 10
{
x.as_bytes() // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_append_1 verus_code! {
use vstd::view::*;
use vstd::string::*;
use vstd::prelude::*;
fn foo() -> (ret: String)
ensures ret@ === ("hello world")@
{
proof {
reveal_strlit("hello world");
reveal_strlit("hello ");
reveal_strlit("world");
}
let mut s = ("hello ").to_string();
s.append(("world"));
assert(s@ =~= ("hello world")@);
s
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_append_2 verus_code! {
use vstd::view::*;
use vstd::string::*;
use vstd::prelude::*;
fn foo() -> (ret: String)
ensures ret@ !== ("hello worlds")@
{
proof {
reveal_strlit("hello worlds");
reveal_strlit("hello ");
reveal_strlit("world");
}
let mut s = ("hello ").to_string();
s.append(("world"));
assert(s@ !~= ("hello worlds")@);
s
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_concat_1 verus_code! {
use vstd::view::*;
use vstd::string::*;
use vstd::prelude::*;
fn foo() -> (ret: String)
ensures ret@ === ("hello world")@
{
proof {
reveal_strlit("hello world");
reveal_strlit("hello ");
reveal_strlit("world");
}
let s1 = ("hello ").to_string();
let s = s1.concat(("world"));
assert(s@ =~= ("hello world")@);
s
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_concat_2 verus_code! {
use vstd::view::*;
use vstd::string::*;
use vstd::prelude::*;
fn foo() -> (ret: String)
ensures ret@ !== ("hello worlds")@
{
proof {
reveal_strlit("hello worlds");
reveal_strlit("hello ");
reveal_strlit("world");
}
let s1 = ("hello ").to_string();
let s = s1.concat(("world"));
assert(s@ !~= ("hello worlds")@);
s
}
} => Ok(())
}
test_verify_one_file! {
#[test] char_clipping_and_ranges verus_code! {
fn test_char_to_u32(c: char) {
let i = c as u32;
assert((0 <= i && i <= 0xD7FF) || (0xE000 <= i && i <= 0x10FFFF));
}
fn test_char_to_u32_fail(c: char) {
let i = c as u32;
assert(i != 0); // FAILS
}
fn test_char_to_u32_fail2(c: char) {
let i = c as u32;
assert(i != 0xD7FF); // FAILS
}
fn test_char_to_u32_fail3(c: char) {
let i = c as u32;
assert(i != 0xE000); // FAILS
}
fn test_char_to_u32_fail4(c: char) {
let i = c as u32;
assert(i != 0x10FFFF); // FAILS
}
proof fn test_char_to_int(c: char) {
let i = c as int;
assert((0 <= i && i <= 0xD7ff) || (0xE000 <= i && i <= 0x10FFFF));
}
proof fn test_char_to_int_fail(c: char) {
let i = c as int;
assert(i != 0); // FAILS
}
proof fn test_char_to_int_fail2(c: char) {
let i = c as int;
assert(i != 0xD7FF); // FAILS
}
proof fn test_char_to_int_fail3(c: char) {
let i = c as int;
assert(i != 0xE000); // FAILS
}
proof fn test_char_to_int_fail4(c: char) {
let i = c as int;
assert(i != 0x10FFFF); // FAILS
}
fn test_ineq(a: char, b: char) {
let bool1 = a <= b;
let bool2 = (a as u32) <= (b as u32);
assert(bool1 == bool2);
}
proof fn test_ineq_pf(a: char, b: char) {
let bool1 = a <= b;
let bool2 = (a as u32) <= (b as u32);
assert(bool1 == bool2);
}
fn test_cast_u8_to_char(x: u8) {
let c = x as char;
assert('\0' <= c && c <= (255 as char));
assert(0 <= c && c <= 255);
}
fn test_cast_u8_to_char_fail(x: u8) {
let c = x as char;
assert(c != 255); // FAILS
}
// Casting any int type to char is not supported in normal Rust (which only allows u8 -> char)
// But it's ok in spec code
proof fn test_cast_u32_to_char(x: u32) {
let c = x as char;
assert((0 <= c && c <= 0xD7FF) || (0xE000 <= c && c <= 0x10FFFF));
}
proof fn test_cast_u32_to_char_fails(x: u32) {
let c = x as char;
assert(c == x); // FAILS
}
proof fn test_cast_i32_to_char(x: i32) {
let c = x as char;
assert((0 <= c && c <= 0xD7FF) || (0xE000 <= c && c <= 0x10FFFF));
}
proof fn test_cast_i32_to_char_fails(x: i32) {
let c = x as char;
assert(c == x); // FAILS
}
proof fn test_cast_int_to_char(x: int) {
let c = x as char;
assert((0 <= c && c <= 0xD7FF) || (0xE000 <= c && c <= 0x10FFFF));
assert(((0 <= x && x <= 0xD7FF) || (0xE000 <= x && x <= 0x10FFFF)) ==> c == x);
}
proof fn test_cast_int_to_char_fails(x: int) {
let c = x as char;
assert(c == x); // FAILS
}
proof fn test_cast_int_to_char_fails2(x: int) {
let c = x as char;
assert(c != 0); // FAILS
}
proof fn test_cast_int_to_char_fails3(x: int) {
let c = x as char;
assert(c != 0xD7FF); // FAILS
}
proof fn test_cast_int_to_char_fails4(x: int) {
let c = x as char;
assert(c != 0xE000); // FAILS
}
proof fn test_cast_int_to_char_fails5(x: int) {
let c = x as char;
assert(c != 0x10FFFF); // FAILS
}
proof fn test_cast_int_to_char_fails6(x: int) {
let c = x as char;
assert(x == 0xD800 ==> c == x); // FAILS
}
proof fn test_cast_int_to_char_fails7(x: int) {
let c = x as char;
assert(x == 0xDFFF ==> c == x); // FAILS
}
proof fn test_cast_int_to_char_fails8(x: int) {
let c = x as char;
assert(x == 0x110000 ==> c == x); // FAILS
}
spec fn char_range_match(c: char) -> bool {
match c {
'\0' ..= '\u{D7FF}' => false,
'\u{E000}' ..= '\u{10FFFF}' => true,
}
}
proof fn test_char_range_match(c: char) {
let x = char_range_match(c);
assert(x ==> c >= 0xDEEE);
}
} => Err(err) => assert_fails(err, 19)
}
test_verify_one_file! {
#[test] test_reveal_empty_string_issue1240 verus_code! {
use vstd::*;
use vstd::string::*;
pub fn test() {
proof { reveal_strlit(""); }
let mut res = String::from_str("");
assert(res@ =~= seq![]);
}
pub fn test2() {
proof { reveal_strlit(""); }
let mut res = String::from_str("");
assert(res@ =~= seq![]);
assert(false); // FAILS
}
} => Err(err) => assert_fails(err, 1)
}