Skip to content

Commit

Permalink
test: fuzzing / property based test cursor contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Oct 10, 2023
1 parent 07c452a commit 2c6249b
Show file tree
Hide file tree
Showing 5 changed files with 590 additions and 34 deletions.
109 changes: 75 additions & 34 deletions contracts/hiro-kit/hk-cursor-v1.clar
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
(define-read-only (read-uint-8 (cursor { bytes: (buff 8192), pos: uint }))
;; Title: hiro-kit-cursor
;; Version: v1

(define-read-only (read-buff-1 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (buff-to-uint-be (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u1)) (err u1)) u1) (err u1))),
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u1)) (err u1)) u1) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u1) }
}))

(define-read-only (read-uint-16 (cursor { bytes: (buff 8192), pos: uint }))
(define-read-only (read-buff-2 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (buff-to-uint-be (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u2)) (err u1)) u2) (err u1))),
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u2)) (err u1)) u2) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u2) }
}))

(define-read-only (read-uint-32 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (buff-to-uint-be (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u4)) (err u1)) u4) (err u1))),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u4) }
}))

(define-read-only (read-uint-64 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (buff-to-uint-be (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u8)) (err u1)) u8) (err u1))),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u8) }
}))

(define-read-only (read-uint-128 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (buff-to-uint-be (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u16)) (err u1)) u16) (err u1))),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u16) }
}))

(define-read-only (read-buff-1 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u1)) (err u1)) u1) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u1) }
}))

(define-read-only (read-buff-4 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u4)) (err u1)) u4) (err u1)),
Expand All @@ -46,6 +25,12 @@
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u8) }
}))

(define-read-only (read-buff-16 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u16)) (err u1)) u16) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u16) }
}))

(define-read-only (read-buff-20 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u20)) (err u1)) u20) (err u1)),
Expand All @@ -58,12 +43,74 @@
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u32) }
}))

(define-read-only (read-buff-64 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u64)) (err u1)) u64) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u64) }
}))

(define-read-only (read-buff-65 (cursor { bytes: (buff 8192), pos: uint }))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) u65)) (err u1)) u65) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) u65) }
}))

(define-read-only (read-uint-8 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-1 cursor))))
(ok (merge cursor-bytes { value: (buff-to-uint-be (get value cursor-bytes)) }))))

(define-read-only (read-uint-16 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-2 cursor))))
(ok (merge cursor-bytes { value: (buff-to-uint-be (get value cursor-bytes)) }))))

(define-read-only (read-uint-32 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-4 cursor))))
(ok (merge cursor-bytes { value: (buff-to-uint-be (get value cursor-bytes)) }))))

(define-read-only (read-uint-64 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-8 cursor))))
(ok (merge cursor-bytes { value: (buff-to-uint-be (get value cursor-bytes)) }))))

(define-read-only (read-uint-128 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-16 cursor))))
(ok (merge cursor-bytes { value: (buff-to-uint-be (get value cursor-bytes)) }))))

(define-read-only (read-int-8 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-1 cursor))))
(ok (merge
cursor-bytes
{ value: (bit-shift-right (bit-shift-left (buff-to-int-be (get value cursor-bytes)) u120) u120) }))))

(define-read-only (read-int-16 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-2 cursor))))
(ok (merge
cursor-bytes
{ value: (bit-shift-right (bit-shift-left (buff-to-int-be (get value cursor-bytes)) u112) u112) }))))

(define-read-only (read-int-32 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-4 cursor))))
(ok (merge
cursor-bytes
{ value: (bit-shift-right (bit-shift-left (buff-to-int-be (get value cursor-bytes)) u96) u96) }))))

(define-read-only (read-int-64 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-8 cursor))))
(ok (merge
cursor-bytes
{ value: (bit-shift-right (bit-shift-left (buff-to-int-be (get value cursor-bytes)) u64) u64) }))))

(define-read-only (read-int-128 (cursor { bytes: (buff 8192), pos: uint }))
(let ((cursor-bytes (try! (read-buff-16 cursor))))
(ok (merge
cursor-bytes
{ value: (bit-shift-right (bit-shift-left (buff-to-int-be (get value cursor-bytes)) u0) u0) }))))

(define-read-only (read-buff-max-len-255 (cursor { bytes: (buff 255), pos: uint }) (actual-len uint))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) actual-len)) (err u1)) u255) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) actual-len) }
}))

(define-read-only (read-buff-max-len-1024 (cursor { bytes: (buff 8192), pos: uint }) (actual-len uint))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) actual-len)) (err u1)) u1024) (err u1)),
Expand All @@ -82,12 +129,6 @@
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) actual-len) }
}))

(define-read-only (read-buff-max-len-255 (cursor { bytes: (buff 255), pos: uint }) (actual-len uint))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) actual-len)) (err u1)) u255) (err u1)),
next: { bytes: (get bytes cursor), pos: (+ (get pos cursor) actual-len) }
}))

(define-read-only (read-buff-max-len-65535 (cursor { bytes: (buff 65535), pos: uint }) (actual-len uint))
(ok {
value: (unwrap! (as-max-len? (unwrap! (slice? (get bytes cursor) (get pos cursor) (+ (get pos cursor) actual-len)) (err u1)) u65535) (err u1)),
Expand Down
64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
"@stacks/transactions": "^6.9.0",
"vitest": "^0.34.1",
"vitest-environment-clarinet": "^0.2.0"
},
"devDependencies": {
"@fast-check/vitest": "^0.0.8",
"fast-check": "^3.13.1"
}
}
Loading

0 comments on commit 2c6249b

Please sign in to comment.