Skip to content

Commit d3e99da

Browse files
lgalabrupavitthrap
authored andcommitted
fix: cargo fmt + tests
1 parent 038a2fa commit d3e99da

File tree

6 files changed

+147
-202
lines changed

6 files changed

+147
-202
lines changed

src/vm/analysis/type_checker/natives/sequences.rs

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -405,60 +405,31 @@ pub fn check_special_slice(
405405
) -> TypeResult {
406406
check_argument_count(3, args)?;
407407

408-
// Position
409-
let position = match args[1].expr {
410-
SymbolicExpressionType::LiteralValue(Value::UInt(position)) => position,
411-
_ => {
412-
let position_type = checker.type_check(&args[1], context)?;
413-
return Err(CheckErrors::TypeError(TypeSignature::UIntType, position_type).into());
414-
}
415-
};
416-
checker
417-
.type_map
418-
.set_type(&args[1], TypeSignature::UIntType)?;
419-
420-
let position = u32::try_from(position).map_err(|_e| CheckErrors::MaxLengthOverflow)?;
421-
422-
// Length
423-
let length = match args[2].expr {
424-
SymbolicExpressionType::LiteralValue(Value::UInt(expected_len)) => expected_len,
425-
_ => {
426-
let expected_len_type = checker.type_check(&args[2], context)?;
427-
return Err(CheckErrors::TypeError(TypeSignature::UIntType, expected_len_type).into());
428-
}
429-
};
430-
checker
431-
.type_map
432-
.set_type(&args[2], TypeSignature::UIntType)?;
433-
434-
let length = u32::try_from(length).map_err(|_e| CheckErrors::MaxLengthOverflow)?;
435-
436-
// Sequence
408+
// Check sequence
437409
let seq_type = checker.type_check(&args[0], context)?;
438-
439-
let (origin_len, resized_seq) = match &seq_type {
440-
TypeSignature::SequenceType(ListType(list)) => (
441-
list.get_max_len(),
442-
TypeSignature::list_of(list.get_list_item_type().clone(), length)?,
443-
),
444-
TypeSignature::SequenceType(BufferType(len)) => (
445-
len.0,
446-
TypeSignature::SequenceType(BufferType(BufferLength(length))),
447-
),
448-
TypeSignature::SequenceType(StringType(ASCII(len))) => (
449-
len.0,
450-
TypeSignature::SequenceType(StringType(ASCII(BufferLength(length)))),
451-
),
452-
TypeSignature::SequenceType(StringType(UTF8(len))) => (
453-
len.0,
454-
TypeSignature::SequenceType(StringType(UTF8(StringUTF8Length(length)))),
455-
),
410+
let seq = match &seq_type {
411+
TypeSignature::SequenceType(seq) => TypeSignature::SequenceType(seq.clone()),
456412
_ => return Err(CheckErrors::ExpectedSequence(seq_type.clone()).into()),
457413
};
414+
// TODO: runtime_cost
458415

459-
if (position + length) > origin_len {
460-
return TypeSignature::new_option(TypeSignature::NoType).map_err(|e| e.into());
461-
}
416+
// Check position argument
417+
let position = checker.type_check(&args[1], context)?;
418+
match position {
419+
TypeSignature::UIntType => Ok(()),
420+
_ => Err(CheckErrors::TypeError(TypeSignature::UIntType, position)),
421+
}?;
422+
// TODO: runtime_cost
423+
424+
// Check length argument
425+
let length = checker.type_check(&args[2], context)?;
426+
match length {
427+
TypeSignature::UIntType => Ok(()),
428+
_ => Err(CheckErrors::TypeError(TypeSignature::UIntType, length)),
429+
}?;
430+
// TODO: runtime_cost
431+
432+
// TODO: analysis_typecheck_cost
462433

463-
Ok(resized_seq)
434+
Ok(seq)
464435
}

src/vm/analysis/type_checker/tests/mod.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,17 @@ fn test_slice_list() {
10811081
let good = [
10821082
"(slice (list 2 3 4 5 6 7 8) u0 u3)",
10831083
"(slice (list u0 u1 u2 u3 u4) u3 u2)",
1084+
"(slice (list 2 3 4 5 6 7 8) u0 u0)",
1085+
"(slice (list 2 3 4 5 6 7 8) u10 u3)",
1086+
"(slice (list) u0 u3)",
1087+
];
1088+
let expected = [
1089+
"(list 7 int)",
1090+
"(list 5 uint)",
1091+
"(list 7 int)",
1092+
"(list 7 int)",
1093+
"(list 0 UnknownType)",
10841094
];
1085-
let expected = ["(list 3 int)", "(list 2 uint)"];
10861095

10871096
for (good_test, expected) in good.iter().zip(expected.iter()) {
10881097
assert_eq!(
@@ -1113,7 +1122,7 @@ fn test_slice_buff() {
11131122
"(slice 0x000102030405 u0 u3)",
11141123
"(slice 0x000102030405 u3 u2)",
11151124
];
1116-
let expected = ["(buff 3)", "(buff 2)"];
1125+
let expected = ["(buff 6)", "(buff 6)"];
11171126

11181127
for (good_test, expected) in good.iter().zip(expected.iter()) {
11191128
assert_eq!(
@@ -1144,7 +1153,7 @@ fn test_slice_ascii() {
11441153
"(slice \"blockstack\" u4 u5)",
11451154
"(slice \"blockstack\" u0 u5)",
11461155
];
1147-
let expected = ["(string-ascii 5)", "(string-ascii 5)"];
1156+
let expected = ["(string-ascii 10)", "(string-ascii 10)"];
11481157

11491158
for (good_test, expected) in good.iter().zip(expected.iter()) {
11501159
assert_eq!(
@@ -1175,7 +1184,7 @@ fn test_slice_utf8() {
11751184
"(slice u\"blockstack\" u4 u5)",
11761185
"(slice u\"blockstack\" u4 u5)",
11771186
];
1178-
let expected = ["(string-utf8 5)", "(string-utf8 5)"];
1187+
let expected = ["(string-utf8 10)", "(string-utf8 10)"];
11791188

11801189
for (good_test, expected) in good.iter().zip(expected.iter()) {
11811190
assert_eq!(

src/vm/docs/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,10 @@ const SLICE_API: SpecialAPI = SpecialAPI {
822822
description:
823823
"The `slice` function returns a sub-sequence of size `length` in the provided sequence.
824824
If `length` is 0 or `position + length` is greater than or equal to `(len sequence)`, this function returns `none`.",
825-
example: "(slice \"blockstack\" u5 u5) ;; Returns (some \"stack\")
826-
(slice (list 1 2 3 4 5) u5 u2) ;; Returns none
827-
(slice (list 1 2 3 4 5) u3 u1) ;; Returns (some (4))
828-
(slice \"abcd\" u1 u2) ;; Returns (some \"bc\")
825+
example: "(slice \"blockstack\" u5 u5) ;; Returns \"stack\"
826+
(slice (list 1 2 3 4 5) u5 u2) ;; Returns ()
827+
(slice (list 1 2 3 4 5) u3 u1) ;; Returns (4)
828+
(slice \"abcd\" u1 u2) ;; Returns \"bc\"
829829
",
830830
};
831831

src/vm/functions/sequences.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,7 @@ pub fn special_slice(
319319
) -> Result<Value> {
320320
check_argument_count(3, args)?;
321321

322-
runtime_cost(
323-
ClarityCostFunction::Concat,
324-
env,
325-
0,
326-
)?;
322+
// TODO: runtime_cost
327323

328324
let seq = eval(&args[0], env, context)?;
329325
let position = eval(&args[1], env, context)?;
@@ -336,12 +332,9 @@ pub fn special_slice(
336332
_ => return Ok(Value::none()),
337333
};
338334

339-
match seq.slice(position, length) {
340-
Ok(v) => Value::some(v)?,
341-
Err(_) => Value::none(),
342-
}
335+
seq.slice(position, length)
343336
}
344337
_ => return Err(RuntimeErrorType::BadTypeConstruction.into()),
345-
};
338+
}?;
346339
Ok(sliced_seq)
347340
}

src/vm/tests/sequences.rs

Lines changed: 75 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -453,137 +453,90 @@ fn test_slice_list() {
453453
let tests = [
454454
"(slice (list 2 3 4 5 6 7 8) u0 u3)",
455455
"(slice (list u0 u1 u2 u3 u4) u3 u2)",
456-
// "(append (append (list) 1) 2)",
456+
"(slice (list 2 3 4 5 6 7 8) u0 u0)",
457+
"(slice (list 2 3 4 5 6 7 8) u10 u3)",
457458
];
458459

459460
let expected = [
460-
Value::some(Value::list_from(vec![Value::Int(2), Value::Int(3), Value::Int(4)]).unwrap())
461-
.unwrap(),
462-
Value::some(Value::list_from(vec![Value::UInt(3), Value::UInt(4)]).unwrap()).unwrap(),
463-
// Value::list_from(vec![Value::Int(1), Value::Int2)]).unwrap(),
461+
Value::list_from(vec![Value::Int(2), Value::Int(3), Value::Int(4)]).unwrap(),
462+
Value::list_from(vec![Value::UInt(3), Value::UInt(4)]).unwrap(),
463+
Value::list_from(vec![]).unwrap(),
464+
Value::list_from(vec![]).unwrap(),
465+
];
466+
467+
for (test, expected) in tests.iter().zip(expected.iter()) {
468+
assert_eq!(expected.clone(), execute(test).unwrap().unwrap());
469+
}
470+
}
471+
472+
#[test]
473+
fn test_slice_buff() {
474+
let tests = [
475+
"(slice 0x000102030405 u0 u3)",
476+
"(slice 0x000102030405 u3 u3)",
477+
"(slice 0x000102030405 u3 u10)",
478+
"(slice 0x000102030405 u10 u3)",
479+
"(slice 0x u2 u3)",
480+
];
481+
482+
let expected = [
483+
Value::buff_from(vec![0, 1, 2]).unwrap(),
484+
Value::buff_from(vec![3, 4, 5]).unwrap(),
485+
Value::buff_from(vec![]).unwrap(),
486+
Value::buff_from(vec![]).unwrap(),
487+
Value::buff_from(vec![]).unwrap(),
488+
];
489+
490+
for (test, expected) in tests.iter().zip(expected.iter()) {
491+
assert_eq!(expected.clone(), execute(test).unwrap().unwrap());
492+
}
493+
}
494+
495+
#[test]
496+
fn test_slice_ascii() {
497+
let tests = [
498+
"(slice \"blockstack\" u0 u5)",
499+
"(slice \"blockstack\" u5 u5)",
500+
"(slice \"blockstack\" u5 u0)",
501+
"(slice \"blockstack\" u11 u3)",
502+
"(slice \"\" u0 u3)",
503+
];
504+
505+
let expected = [
506+
Value::string_ascii_from_bytes("block".into()).unwrap(),
507+
Value::string_ascii_from_bytes("stack".into()).unwrap(),
508+
Value::string_ascii_from_bytes(vec![]).unwrap(),
509+
Value::string_ascii_from_bytes(vec![]).unwrap(),
510+
Value::string_ascii_from_bytes(vec![]).unwrap(),
464511
];
465512

466513
for (test, expected) in tests.iter().zip(expected.iter()) {
467514
assert_eq!(expected.clone(), execute(test).unwrap().unwrap());
468515
}
516+
}
517+
518+
#[test]
519+
fn test_slice_utf8() {
520+
let tests = [
521+
"(slice u\"hello \\u{1F98A}\" u0 u5)",
522+
"(slice u\"hello \\u{1F98A}\" u6 u1)",
523+
"(slice u\"hello \\u{1F98A}\" u6 u0)",
524+
"(slice u\"hello \\u{1F98A}\" u11 u4)",
525+
"(slice u\"\" u0 u3)",
526+
];
527+
528+
let expected = [
529+
Value::string_utf8_from_bytes("hello".into()).unwrap(),
530+
Value::string_utf8_from_bytes("🦊".into()).unwrap(),
531+
Value::string_utf8_from_bytes(vec![]).unwrap(),
532+
Value::string_utf8_from_bytes(vec![]).unwrap(),
533+
Value::string_utf8_from_bytes(vec![]).unwrap(),
534+
];
469535

470-
// assert_eq!(
471-
// execute("(append (append (list) 1) u2)").unwrap_err(),
472-
// CheckErrors::TypeValueError(IntType, Value::UInt(2)).into()
473-
// );
474-
}
475-
476-
// #[test]
477-
// fn test_slice_list() {
478-
// let good = ["(slice (list 2 3 4 5 6 7 8) u0 u3)", "(slice (list u0 u1 u2 u3 u4) u3 u2)"];
479-
// let expected = ["(list 3 int)", "(list 2 uint)"];
480-
481-
// for (good_test, expected) in good.iter().zip(expected.iter()) {
482-
// assert_eq!(
483-
// expected,
484-
// &format!("{}", type_check_helper(&good_test).unwrap())
485-
// );
486-
// }
487-
488-
// let bad = [
489-
// "(slice (list 2 3) 3 u4)",
490-
// "(slice (list 2 3) u3 4)",
491-
// "(slice (list u0) u1)",
492-
// ];
493-
494-
// let bad_expected = [
495-
// CheckErrors::TypeError(UIntType, IntType),
496-
// CheckErrors::TypeError(UIntType, IntType),
497-
// CheckErrors::IncorrectArgumentCount(3, 2),
498-
// ];
499-
// for (bad_test, expected) in bad.iter().zip(bad_expected.iter()) {
500-
// assert_eq!(expected, &type_check_helper(&bad_test).unwrap_err().err);
501-
// }
502-
// }
503-
504-
// #[test]
505-
// fn test_slice_buff() {
506-
// let good = ["(slice 0x000102030405 u0 u3)", "(slice 0x000102030405 u3 u2)"];
507-
// let expected = ["(buff 3)", "(buff 2)"];
508-
509-
// for (good_test, expected) in good.iter().zip(expected.iter()) {
510-
// assert_eq!(
511-
// expected,
512-
// &format!("{}", type_check_helper(&good_test).unwrap())
513-
// );
514-
// }
515-
516-
// let bad = [
517-
// "(slice 0x000102030405 3 u4)",
518-
// "(slice 0x000102030405 u3 4)",
519-
// "(slice 0x000102030405 u1)",
520-
// ];
521-
522-
// let bad_expected = [
523-
// CheckErrors::TypeError(UIntType, IntType),
524-
// CheckErrors::TypeError(UIntType, IntType),
525-
// CheckErrors::IncorrectArgumentCount(3, 2),
526-
// ];
527-
// for (bad_test, expected) in bad.iter().zip(bad_expected.iter()) {
528-
// assert_eq!(expected, &type_check_helper(&bad_test).unwrap_err().err);
529-
// }
530-
// }
531-
532-
// #[test]
533-
// fn test_slice_ascii() {
534-
// let good = ["(slice \"blockstack\" u4 u5)", "(slice \"blockstack\" u0 u5)"];
535-
// let expected = ["(string-ascii 5)", "(string-ascii 5)"];
536-
537-
// for (good_test, expected) in good.iter().zip(expected.iter()) {
538-
// assert_eq!(
539-
// expected,
540-
// &format!("{}", type_check_helper(&good_test).unwrap())
541-
// );
542-
// }
543-
544-
// let bad = [
545-
// "(slice \"blockstack\" 3 u4)",
546-
// "(slice \"blockstack\" u3 4)",
547-
// "(slice \"blockstack\" u1)",
548-
// ];
549-
550-
// let bad_expected = [
551-
// CheckErrors::TypeError(UIntType, IntType),
552-
// CheckErrors::TypeError(UIntType, IntType),
553-
// CheckErrors::IncorrectArgumentCount(3, 2),
554-
// ];
555-
// for (bad_test, expected) in bad.iter().zip(bad_expected.iter()) {
556-
// assert_eq!(expected, &type_check_helper(&bad_test).unwrap_err().err);
557-
// }
558-
// }
559-
560-
// #[test]
561-
// fn test_slice_utf8() {
562-
// let good = ["(slice u\"blockstack\" u4 u5)", "(slice u\"blockstack\" u4 u5)"];
563-
// let expected = ["(string-utf8 5)", "(string-utf8 5)"];
564-
565-
// for (good_test, expected) in good.iter().zip(expected.iter()) {
566-
// assert_eq!(
567-
// expected,
568-
// &format!("{}", type_check_helper(&good_test).unwrap())
569-
// );
570-
// }
571-
572-
// let bad = [
573-
// "(slice u\"blockstack\" 3 u4)",
574-
// "(slice u\"blockstack\" u3 4)",
575-
// "(slice u\"blockstack\" u1)",
576-
// ];
577-
578-
// let bad_expected = [
579-
// CheckErrors::TypeError(UIntType, IntType),
580-
// CheckErrors::TypeError(UIntType, IntType),
581-
// CheckErrors::IncorrectArgumentCount(3, 2),
582-
// ];
583-
// for (bad_test, expected) in bad.iter().zip(bad_expected.iter()) {
584-
// assert_eq!(expected, &type_check_helper(&bad_test).unwrap_err().err);
585-
// }
586-
// }
536+
for (test, expected) in tests.iter().zip(expected.iter()) {
537+
assert_eq!(expected.clone(), execute(test).unwrap().unwrap());
538+
}
539+
}
587540

588541
#[test]
589542
fn test_simple_list_concat() {

0 commit comments

Comments
 (0)