question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how to create dependent properties?

See original GitHub issue

Hi all,

I want to write a test for a method that takes a non-empty collection of positive numbers double[], and a double targetSum, and finds the shortest combination of numbers in the collection, whose sum is >= targetSum * 0.95 and <= targetSum * 1.15.

My attempt looks like this:

public static class Generators
{
    public static Arbitrary<double[]> Doubles()
    {
        return Gen.NonEmptyListOf(Arb.Generate<double>()
            .Where(x => x >= 2 && x <= 20))
            .Select(x => x.ToArray()).ToArbitrary();
    }
}

public class Tests
{
    [Property(Arbitrary = new[] { typeof(Generators) })]
    public void FindBestSessionsTest(double[] values, double targetSum)
    {
        var result = FindBestCombination(values, targetSum);

        result.Should().NotBeEmpty();
        var sum = result.Sum();
        sum.Should().BeGreaterThanOrEqualTo(targetSum * 0.95)
            .And.BeLessThanOrEqualTo(targetSum * 1.15);
    }
}

I cannot figure out how to create a generator that will create a targetSum. The strategy I think should work is to pick one or more numbers from the values input array, sum them together, and then take the resulting exact targetSum and shift it up/down within the legal range targetSum * 0.95 and targetSum * 1.15. My idea is that FsCheck should try to find a targetSum that should be possible for FindBestCombination to find but cannot.

Any suggestions would be much appreciated by an FsCheck newbie!

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ploehcommented, Apr 4, 2023

I don’t think there’s anything wrong with the specific solution you’ve arrived at. I just find problems like these interesting because they seem to expose that there are more layers beneath the first one.

The approach that you’re currently taking is the first layer, and, again, there’s nothing wrong with it. Frequently, I stop there. Essentially, what you’re doing is that you’ve identified an equivalence class and now the problem has become: How to draw inputs from that equivalence class as faithfully as possible.

Sometimes, a next level is available. This is where you identify properties that hold for the entire domain of the function. This relieves you from the often awkward arrangement of the equivalence class, so it makes the tests simpler and also tends to make them more robust to changes. My blog has an example: Property-based testing is not the same as partition testing.

A third layer then sometimes follows where you begin to realise that (part of) what remains in terms of expressing the domain of the function can be expressed as types rather than FsCheck combinators. (Trivial example: Use a (hypothetical) NonEmptyCollection<T> instead of an array.) This once again makes the tests simpler, and also the SUT safer (à la poka-yoke).

Based on what you can divulge, I can’t see what opportunities exist, so we can just leave it at that.

1reaction
kurtschelfthoutcommented, Apr 3, 2023

@Egil mostly a question of preference? Sometimes (as in the first iteration) one or the other turns out much simpler. I haven’t put much deep thought in it. I think @ploeh might have a much more considered opinion or alternative.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create dependent properties ? - Autodesk Community
I have a question about how I can make a property dependent on other properties. See my attached image, here I want property...
Read more >
Use dependent form fields
Add dependent fields to a form · Click the If [property] dropdown menu and select the targeting rule for your dependent field. ·...
Read more >
C# Class with dependent properties
My goal is to have something like this: an app that gives the results as soon as you type a value in one...
Read more >
How to efficiently use dependent properties if ...
I want to design a class where some properties are dependent on others. I understand the general concept of Matlab OOP for dependent...
Read more >
Dependent properties vs functions - MATLAB Answers
Both methods and dependent properties allow to make computations "on the fly", instead of storing the values into the object. However, replacing a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found