Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Hopefully "fun" problem to solve - matrix of test values

I need some help conceptualizing (and realizing) a matrix of test values.

I want to test all possible permutations for a series of 6 permissions.  Each permission can be true or false.

CanCreate,CanRead,CanUpdate,CanDelete,CanUseWebservice,CanRunReports

I think the math is 2^6 or 64 different combinations possible:

1,0,0,0,0,0

would represent granting "Can Create" permission and no permissions on the rest.

1,1,0,0,0,0 would grant CanCreate, CanRead and no on the rest.

I want to cover every combination.

I am looking to create a simple matrix for the combinations, just a CSV file of sorts.


Here is what I have so far, but what is the correct way to do this so that no test values repeat and all combinations are represented?

If I find the answer I'll return here and post it.  Until then I await your answer, gurus!


private void buttonGenerateCSV_Click(object sender, EventArgs e)
        {
            string[] headervals = textBoxHeaderValues.Text.Split(',');
            string[] validvals = textBoxValidValues.Text.Split(',');

            foreach (string hs in headervals)
            {
                textBoxCSV.Text += hs;
                foreach (string s in validvals)
                {
                    textBoxCSV.Text += s;
                }
            }

        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Knowlton

ASKER

Here was my eventual solution:


using System;
using System.Linq;
using System.Windows.Forms;

namespace CSharpCodeGenAndSamples
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string ReturnDecAsBinStringWithPadding(int dectoconvert, int maxpadding)
        {
            string binValue = Convert.ToString(dectoconvert, 2);

            int padlen = binValue.Length;

            while (padlen < maxpadding)
            {
                binValue = "0" + binValue;
                padlen++;
            }

            return binValue;
        }

        private int PowerOf(int num, int basenum)
        {
            int newnum = num;

            for (int i = 1; i < basenum; i++)
            {
                newnum *= num;
            }

            return newnum;
        }

        private string InjectCharsIntoExistingString(string origstring, string chartoinject)
        {
            int strlen = origstring.Length;

            string newstring = "";

            for (int i = 0; i < strlen; i++)
            {
                newstring += origstring[i] + chartoinject;
            }

            newstring = newstring.Substring(0, newstring.Length - 1);
            return newstring;
        }

        private void ButtonGenerateCsvClick(object sender, EventArgs e)
        {
            string[] headervals = textBoxHeaderValues.Text.Split(',');
            string[] validvals = textBoxValidValues.Text.Split(',');

            int countofheaders = headervals.Count();
            int countofvalidvals = validvals.Count();
            int power = PowerOf(countofvalidvals, countofheaders);

            string headercols = headervals.Aggregate("", (current, hs) => current + (hs + ","));

            headercols = headercols.Substring(0, headercols.Length - 1);

            textBoxCSV.Text += headercols + Environment.NewLine;

            for (int i = 0; i < power; i++)
            {
                string binasstring = ReturnDecAsBinStringWithPadding(i, 6);
                string arrayofbinvals = InjectCharsIntoExistingString(binasstring, ",");

                textBoxCSV.Text += arrayofbinvals + Environment.NewLine;
                //string[] newarrayofbinvals = arrayofbinvals.Split(',');                
            }


            foreach (string s in validvals)
            {
                textBoxCSV.Text += s;
                foreach (string hs in headervals)
                {
                    textBoxCSV.Text += hs;
                }
            }
        }
    }
}

Open in new window

Thank you for the example and for your time spent on this!

Tom
No problem.  Definitely many ways to approach this problem...