Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds test cases for sum and delta system macros #137

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 79 additions & 7 deletions conformance/system_macros/delta.ion
Original file line number Diff line number Diff line change
@@ -1,12 +1,84 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Test Cases:
// delta can be invoked using any type of macro reference.
// the arguments of delta must be non-null integers
// the "initial" argument is required
// the "delta" argument can be zero or more values
// any annotations on the arguments are silently dropped
(ion_1_1 "delta can be invoked"
(each "in text with an unqualified macro name"
(text "(:delta)")
"in text with an unqualified macro address"
(text "(:18)")
"in text with a qualified macro name"
(text "(:$ion::delta)")
"in text with a qualified macro address"
(text "(:$ion::18)")
"in binary with a system macro address"
(binary "EF 12 00")
"in binary with a user macro address"
(binary "12 00")
(produces)))

// the output of delta is defined as follows:
// output₀ = initial + delta₀
// output₀ = delta₀
// outputₙ₊₁ = outputₙ + deltaₙ₊₁
(ion_1_1 "delta produces a stream of values that is the delta of"
(each "0 arguments"
(binary "EF 12 00")
(text "(:delta)")
(text "(:delta (::))")
(produces))
(each "1 argument"
(binary "EF 12 01 03")
(text "(:delta 1)")
(produces 1))
(each "2 arguments"
(binary "EF 12 02 05 03 05 01")
(text "(:delta 1 2)")
(produces 1 3))
(each "3 arguments"
(binary "EF 12 02 07 FF FF FF 01")
(text "(:delta -1 -1 -1)")
(produces -1 -2 -3))
(each "4 arguments"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you throw in one or two simple tests showing that delta-of-delta works too?

(binary "EF 12 02 09 03 03 03 03 01")
(text "(:delta 1 1 1 1)")
(text "(:delta (:repeat 4 1))")
(produces 1 2 3 4))
(each "many arguments"
(text "(:delta 1 2 3 4 5 -1 -2 -3)")
// Annotations are silently dropped
(text "(:delta a::1 b::2 c::3 d::4 e::5 -1 -2 -3)")
(produces 1 3 6 10 15 14 12 9)))

(ion_1_1 "arguments may not be"
(each "any null"
(text "(:delta 1 null 2)")
(text "(:delta 1 null.int 2)")
(text "(:delta 1 null.float 2)")
(text "(:delta 1 null.decimal 2)")
"a float"
(text "(:delta 1 3e1 2)")
"a decimal"
(text "(:delta 1 3d1 2)")
"a list of numbers"
(text "(:delta 1 [3, 4, 5] 2)")
"a sexp of numbers"
(text "(:delta 1 (3 4 5) 2)")
"a numeric string"
(text "(:delta 1 '''3''' 2)")
(signals "invalid argument")))

// Demonstrations of some interesting things you can do with delta.

(ion_1_1 "delta and repeat can be combined to generate"
(mactab $ion_encoding (macro from_x_count_n_by_step (x n step) (.delta (.. (%x) (.repeat (%n) (%step))))))
(then "an increasing sequence" (text "(:from_x_count_n_by_step 0 10 1)") (produces 0 1 2 3 4 5 6 7 8 9 10))
(then "a count-by-twos sequence" (text "(:from_x_count_n_by_step 0 10 2)") (produces 0 2 4 6 8 10 12 14 16 18 20))
(then "a decreasing sequence" (text "(:from_x_count_n_by_step 5 10 -1)") (produces 5 4 3 2 1 0 -1 -2 -3 -4 -5)))

(ion_1_1 "it is possible to create a delta of deltas encoding"
// See, for example
// https://www.timescale.com/blog/time-series-compression-algorithms-explained/#delta-of-delta-encoding
(mactab $ion_encoding
(macro delta_of_deltas (flex_int::init dod*) (.delta (.. (%init) (.delta (%dod)))))
(macro rle (flex_uint::run_length flex_int::value) (.repeat (%run_length) (%value))))
(text "(:delta_of_deltas 52 (:: -1 (:rle 10 0) 1 (:rle 5 0) 1 (:rle 5 0)))")
(produces 52 51 50 49 48 47 46 45 44 43 42 41 41 41 41 41 41 41 42 43 44 45 46 47))
86 changes: 81 additions & 5 deletions conformance/system_macros/sum.ion
Original file line number Diff line number Diff line change
@@ -1,8 +1,84 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Test Cases:
// sum can be invoked using any type of macro reference.
// the arguments of sum must be zero or more non-null integers
// any annotations on the arguments are silently dropped
// the output of sum is the sum of all its argument values
(ion_1_1 "sum can be invoked"
(each "in text with an unqualified macro name"
(text "(:sum 0 0)")
"in text with an unqualified macro address"
(text "(:20 0 0)")
"in text with a qualified macro name"
(text "(:$ion::sum 0 0)")
"in text with a qualified macro address"
(text "(:$ion::20 0 0)")
"in binary with a system macro address"
(binary "EF 14 60 60")
"in binary with a user macro address"
(binary "14 60 60")
(produces 0)))

(ion_1_1 "sum produces a single, unannotated integer that is the sum of"
(each "integers"
(text "(:sum -1 4)")
(text "(:sum 0 3)")
(text "(:sum 1 2)")
(text "(:sum 2 1)")
(text "(:sum 3 0)")
(text "(:sum 4 -1)")
"annotated integers"
(text "(:sum a::1 2)")
(text "(:sum 1 b::2)")
(text "(:sum a::1 b::2)")
"expressions that produce integers"
(text "(:sum (:values 1) 2)")
(text "(:sum 1 (:values 2))")
"other sums"
(text "(:sum (:sum 1 1) 1)")
(text "(:sum 1 (:sum 1 1))")
(text "(:sum (:sum -4 3) (:sum 2 2))")
(produces 3)))

(ion_1_1 "sum is commutative"
(each (text "(:sum 1 0)") (text "(:sum 0 1)") (produces 1))
(each (text "(:sum -1 0)") (text "(:sum 0 -1)") (produces -1))
(each (text "(:sum -1 1)") (text "(:sum 1 -1)") (produces 0))
(each (text "(:sum 3 7)") (text "(:sum 7 3)") (produces 10))
(each (text "(:sum -3 -5)") (text "(:sum -5 -3)") (produces -8))
(each (text "(:sum -2 4)") (text "(:sum 4 -2)") (produces 2)))

(ion_1_1 "arguments may not be"
(each "less than two integers"
(text "(:sum)")
(text "(:sum 1)")
"more than two integers"
(text "(:sum 1 2 3)")
"any null"
(text "(:sum 1 null)")
(text "(:sum 1 null.int)")
(text "(:sum 1 null.float)")
(text "(:sum 1 null.decimal)")
(text "(:sum null 2)")
(text "(:sum null.int 2)")
(text "(:sum null.float 2)")
(text "(:sum null.decimal 2)")
"a float"
(text "(:sum 1 3e1)")
(text "(:sum 3e1 2)")
"a decimal"
(text "(:sum 1 3d1)")
(text "(:sum 3d1 2)")
"a list of numbers"
(text "(:sum 1 [3])")
(text "(:sum [3] 2)")
(text "(:sum [1, 2])")
"a sexp of numbers"
(text "(:sum 1 (3))")
(text "(:sum (3) 2)")
(text "(:sum (1 2))")
"a numeric text value"
(text '''(:sum 1 "3")''')
(text '''(:sum 1 '3')''')
(text '''(:sum "3" 2)''')
(text '''(:sum '3' 2)''')
"a single expression that produces two integers"
(text "(:sum (:values 1 3))")
(signals "invalid argument")))
Loading