Skip to content

Commit

Permalink
fix: cargo fmt + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru authored and pavitthrap committed Aug 2, 2021
1 parent 844e5dc commit 038a2fa
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/vm/analysis/tests/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub fn test_tracked_costs(prog: &str) -> ExecutionCost {
(define-constant tuple-foo (tuple (a 1)))
(define-constant list-foo (list true))
(define-constant list-bar (list 1))
(define-constant str-foo \"foobar\")
(use-trait trait-1 .contract-trait.trait-1)
(define-public (execute (contract <trait-1>)) (ok {}))",
prog
Expand Down
33 changes: 18 additions & 15 deletions src/vm/analysis/type_checker/natives/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,25 +437,28 @@ pub fn check_special_slice(
let seq_type = checker.type_check(&args[0], context)?;

let (origin_len, resized_seq) = match &seq_type {
TypeSignature::SequenceType(ListType(list)) => {
(list.get_max_len(), TypeSignature::list_of(list.get_list_item_type().clone(), length)?)
},
TypeSignature::SequenceType(BufferType(len)) => {
(len.0, TypeSignature::SequenceType(BufferType(BufferLength(length))))
},
TypeSignature::SequenceType(StringType(ASCII(len))) => {
(len.0, TypeSignature::SequenceType(StringType(ASCII(BufferLength(length)))))
},
TypeSignature::SequenceType(StringType(UTF8(len))) => {
(len.0, TypeSignature::SequenceType(StringType(UTF8(StringUTF8Length(length)))))
},
TypeSignature::SequenceType(ListType(list)) => (
list.get_max_len(),
TypeSignature::list_of(list.get_list_item_type().clone(), length)?,
),
TypeSignature::SequenceType(BufferType(len)) => (
len.0,
TypeSignature::SequenceType(BufferType(BufferLength(length))),
),
TypeSignature::SequenceType(StringType(ASCII(len))) => (
len.0,
TypeSignature::SequenceType(StringType(ASCII(BufferLength(length)))),
),
TypeSignature::SequenceType(StringType(UTF8(len))) => (
len.0,
TypeSignature::SequenceType(StringType(UTF8(StringUTF8Length(length)))),
),
_ => return Err(CheckErrors::ExpectedSequence(seq_type.clone()).into()),
};

if (position + length) > origin_len {
return Err(CheckErrors::ConstructedListTooLarge.into())
}
return TypeSignature::new_option(TypeSignature::NoType).map_err(|e| e.into());
}

Ok(resized_seq)
}

22 changes: 16 additions & 6 deletions src/vm/analysis/type_checker/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,10 @@ fn test_native_append() {

#[test]
fn test_slice_list() {
let good = ["(slice (list 2 3 4 5 6 7 8) u0 u3)", "(slice (list u0 u1 u2 u3 u4) u3 u2)"];
let good = [
"(slice (list 2 3 4 5 6 7 8) u0 u3)",
"(slice (list u0 u1 u2 u3 u4) u3 u2)",
];
let expected = ["(list 3 int)", "(list 2 uint)"];

for (good_test, expected) in good.iter().zip(expected.iter()) {
Expand Down Expand Up @@ -1106,7 +1109,10 @@ fn test_slice_list() {

#[test]
fn test_slice_buff() {
let good = ["(slice 0x000102030405 u0 u3)", "(slice 0x000102030405 u3 u2)"];
let good = [
"(slice 0x000102030405 u0 u3)",
"(slice 0x000102030405 u3 u2)",
];
let expected = ["(buff 3)", "(buff 2)"];

for (good_test, expected) in good.iter().zip(expected.iter()) {
Expand All @@ -1132,10 +1138,12 @@ fn test_slice_buff() {
}
}


#[test]
fn test_slice_ascii() {
let good = ["(slice \"blockstack\" u4 u5)", "(slice \"blockstack\" u0 u5)"];
let good = [
"(slice \"blockstack\" u4 u5)",
"(slice \"blockstack\" u0 u5)",
];
let expected = ["(string-ascii 5)", "(string-ascii 5)"];

for (good_test, expected) in good.iter().zip(expected.iter()) {
Expand All @@ -1161,10 +1169,12 @@ fn test_slice_ascii() {
}
}


#[test]
fn test_slice_utf8() {
let good = ["(slice u\"blockstack\" u4 u5)", "(slice u\"blockstack\" u4 u5)"];
let good = [
"(slice u\"blockstack\" u4 u5)",
"(slice u\"blockstack\" u4 u5)",
];
let expected = ["(string-utf8 5)", "(string-utf8 5)"];

for (good_test, expected) in good.iter().zip(expected.iter()) {
Expand Down
3 changes: 1 addition & 2 deletions src/vm/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,8 @@ const SLICE_API: SpecialAPI = SpecialAPI {
If `length` is 0 or `position + length` is greater than or equal to `(len sequence)`, this function returns `none`.",
example: "(slice \"blockstack\" u5 u5) ;; Returns (some \"stack\")
(slice (list 1 2 3 4 5) u5 u2) ;; Returns none
(slice (list 1 2 3 4 5) (+ u1 u2) u1) ;; Returns (some (list 4))
(slice (list 1 2 3 4 5) u3 u1) ;; Returns (some (4))
(slice \"abcd\" u1 u2) ;; Returns (some \"bc\")
(slice 0xfb010203 u1 u3) ;; Returns (some 0x010203)
",
};

Expand Down
15 changes: 9 additions & 6 deletions src/vm/functions/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,15 @@ pub fn special_slice(
(Value::Sequence(seq), Value::UInt(position), Value::UInt(length)) => {
let (position, length) = match (usize::try_from(position), usize::try_from(length)) {
(Ok(position), Ok(length)) => (position, length),
_ => return Ok(Value::none())
_ => return Ok(Value::none()),
};

seq.slice(position, length)
},
_ => Err(RuntimeErrorType::BadTypeConstruction.into()),
}?;

match seq.slice(position, length) {
Ok(v) => Value::some(v)?,
Err(_) => Value::none(),
}
}
_ => return Err(RuntimeErrorType::BadTypeConstruction.into()),
};
Ok(sliced_seq)
}
3 changes: 2 additions & 1 deletion src/vm/tests/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn get_simple_test(function: &NativeFunctions) -> &'static str {
StxTransferMemo => r#"(stx-transfer-memo? u1 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x89995432)"#,
StxBurn => "(stx-burn? u1 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)",
StxGetAccount => "(stx-account 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)",
Slice => "(slice list-bar u1 u1)",
Slice => "(slice str-foo u1 u1)",
}
}

Expand Down Expand Up @@ -177,6 +177,7 @@ fn test_tracked_costs(prog: &str) -> ExecutionCost {
(define-constant tuple-foo (tuple (a 1)))
(define-constant list-foo (list true))
(define-constant list-bar (list 1))
(define-constant str-foo \"foobar\")
(use-trait trait-1 .contract-trait.trait-1)
(define-public (execute (contract <trait-1>)) (ok {}))",
prog
Expand Down
8 changes: 3 additions & 5 deletions src/vm/tests/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ fn test_slice_list() {
];

let expected = [
Value::list_from(vec![Value::Int(2), Value::Int(3), Value::Int(4)]).unwrap(),
Value::list_from(vec![Value::UInt(3), Value::UInt(4)]).unwrap(),
Value::some(Value::list_from(vec![Value::Int(2), Value::Int(3), Value::Int(4)]).unwrap())
.unwrap(),
Value::some(Value::list_from(vec![Value::UInt(3), Value::UInt(4)]).unwrap()).unwrap(),
// Value::list_from(vec![Value::Int(1), Value::Int2)]).unwrap(),
];

Expand All @@ -472,7 +473,6 @@ fn test_slice_list() {
// );
}


// #[test]
// fn test_slice_list() {
// let good = ["(slice (list 2 3 4 5 6 7 8) u0 u3)", "(slice (list u0 u1 u2 u3 u4) u3 u2)"];
Expand Down Expand Up @@ -529,7 +529,6 @@ fn test_slice_list() {
// }
// }


// #[test]
// fn test_slice_ascii() {
// let good = ["(slice \"blockstack\" u4 u5)", "(slice \"blockstack\" u0 u5)"];
Expand Down Expand Up @@ -558,7 +557,6 @@ fn test_slice_list() {
// }
// }


// #[test]
// fn test_slice_utf8() {
// let good = ["(slice u\"blockstack\" u4 u5)", "(slice u\"blockstack\" u4 u5)"];
Expand Down
18 changes: 11 additions & 7 deletions src/vm/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,24 @@ impl SequenceData {
}

pub fn slice(self, position: usize, length: usize) -> Result<Value> {
if position + length > self.len() {
if position + length > self.len() {
return Err(RuntimeErrorType::BadTypeConstruction.into());
}
let result = match self {
SequenceData::Buffer(data) => Value::buff_from(data.data[position..(length + position)].to_vec()),
SequenceData::List(data) => Value::list_from(data.data[position..(length + position)].to_vec()),
SequenceData::Buffer(data) => {
Value::buff_from(data.data[position..(length + position)].to_vec())
}
SequenceData::List(data) => {
Value::list_from(data.data[position..(length + position)].to_vec())
}
SequenceData::String(CharType::ASCII(data)) => {
Value::string_ascii_from_bytes(data.data[position..(length + position)].to_vec())
}
SequenceData::String(CharType::UTF8(data)) => {
Ok(Value::Sequence(SequenceData::String(CharType::UTF8(UTF8Data {
SequenceData::String(CharType::UTF8(data)) => Ok(Value::Sequence(
SequenceData::String(CharType::UTF8(UTF8Data {
data: data.data[position..(length + position)].to_vec(),
}))))
}
})),
)),
}?;

Ok(result)
Expand Down

0 comments on commit 038a2fa

Please sign in to comment.