<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shivang Vora</title>
    <description>The latest articles on DEV Community by Shivang Vora (@shivangvora1206).</description>
    <link>https://dev.to/shivangvora1206</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1348758%2Fc65c0f2a-e9d9-4fdc-8001-b9a0f3b85127.png</url>
      <title>DEV Community: Shivang Vora</title>
      <link>https://dev.to/shivangvora1206</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shivangvora1206"/>
    <language>en</language>
    <item>
      <title>A Developer's Guide to Test Case Generation with Genetic Algorithms</title>
      <dc:creator>Shivang Vora</dc:creator>
      <pubDate>Sat, 22 Nov 2025 12:47:42 +0000</pubDate>
      <link>https://dev.to/shivangvora1206/a-developers-guide-to-test-case-generation-with-genetic-algorithms-4mef</link>
      <guid>https://dev.to/shivangvora1206/a-developers-guide-to-test-case-generation-with-genetic-algorithms-4mef</guid>
      <description>&lt;p&gt;As developers, we often face the challenge of testing functions with complex business logic and numerous parameters. How can we be sure we've covered all the tricky edge cases and interactions between different inputs? Manually writing these tests is tedious, and a simple brute-force approach can lead to a combinatorial explosion of test cases.&lt;/p&gt;

&lt;p&gt;This is where a smarter approach, like using a Genetic Algorithm (GA), can be a game-changer. In this article, I'll walk through a project that uses a GA to automatically generate a concise and effective set of test cases for a complex e-commerce pricing function, explaining the core logic step-by-step.&lt;/p&gt;

&lt;h2&gt;
  
  
  The System Under Test: A Complex Pricing Function
&lt;/h2&gt;

&lt;p&gt;To demonstrate the power of the GA, we need a sufficiently complex function. In my experiment, I used a Python function that calculates the final price of a product. It involves various rules: percentage discounts, flat-rate discounts, "Buy One, Get One" (BOGO) offers, membership-level bonuses, and promotional codes.&lt;/p&gt;

&lt;p&gt;Here is the signature and some of the validation logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_final_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;base_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;discount_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;discount_value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;membership_level&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cart_total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;promo_code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;product_category&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# --- A few example validation checks ---
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;base_price&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid base price.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;discount_type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;flat&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;discount_value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;base_price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Flat discount too high.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;promo_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;SAVE10&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;cart_total&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Promo code SAVE10 requires $100+ cart.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;discount_type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;bogo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;product_category&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apparel&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;accessories&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;BOGO only valid for apparel and accessories.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# ... and many more ...
&lt;/span&gt;
    &lt;span class="n"&gt;final_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base_price&lt;/span&gt;
    &lt;span class="c1"&gt;# ... apply discounts based on logic ...
&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;final_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generating test cases for this is difficult. A &lt;code&gt;'bogo'&lt;/code&gt; discount is only valid for &lt;code&gt;'apparel'&lt;/code&gt;, and the &lt;code&gt;'SAVE10'&lt;/code&gt; promo code requires a &lt;code&gt;cart_total&lt;/code&gt; over $100. A random approach would generate mostly invalid inputs that raise &lt;code&gt;ValueError&lt;/code&gt;, failing to test the actual pricing logic. Our goal is to find inputs that &lt;em&gt;pass&lt;/em&gt; these checks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Evolving Valid Test Cases with a Genetic Algorithm
&lt;/h2&gt;

&lt;p&gt;Instead of generating inputs blindly, we can use a GA to "evolve" a population of test cases. The algorithm learns the function's rules and produces a set of "fit" individuals that successfully exercise the logic. Let's break down the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Defining "Fitness"
&lt;/h3&gt;

&lt;p&gt;First, we need to quantify how "good" a test case is. In our scenario, a test case is good if it's &lt;em&gt;valid&lt;/em&gt;—meaning it doesn't cause the function to raise an exception. We create a &lt;code&gt;fitness&lt;/code&gt; function that rewards these valid inputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fitness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;test_case&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Attempt to run the function with the given test case
&lt;/span&gt;        &lt;span class="nf"&gt;calculate_final_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;test_case&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# If it succeeds, the test case is valid and "fit"
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# If it fails, the test case is invalid and "unfit"
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the core of our "learning" process. By giving a higher score (&lt;code&gt;10&lt;/code&gt;) to test cases that run successfully, we tell the algorithm which individuals should be prioritized for creating the next generation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Interestingly, if we flip the fitness logic by setting a higher score for errors, we get output test cases that are the best at failing!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 2: The Genetic Operators - Crossover and Mutation
&lt;/h3&gt;

&lt;p&gt;Genetic Algorithms are inspired by evolution. We need mechanisms for "reproduction" (&lt;code&gt;crossover&lt;/code&gt;) and random change (&lt;code&gt;mutation&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Crossover&lt;/strong&gt; combines two successful parent test cases to create a new "child." Our implementation uses uniform crossover: for each parameter, it randomly chooses the value from one of the two parents. This allows good traits (valid parameter combinations) from different parents to be mixed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;crossover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# For each parameter, randomly choose the value from parent 1 or parent 2
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mutation&lt;/strong&gt; introduces new genetic material into the population, preventing it from getting stuck. Our &lt;code&gt;mutate&lt;/code&gt; function takes a test case and replaces one of its parameters with a new, completely random value. This helps explore new possibilities that crossover alone might not discover.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;mutate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Pick a random parameter index to change
&lt;/span&gt;    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Replace it with a new, fully random value for that parameter type
&lt;/span&gt;    &lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;random_test_case&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: The &lt;code&gt;evolve&lt;/code&gt; Loop - Simulating Natural Selection
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;evolve&lt;/code&gt; function orchestrates the entire process, running the simulation for a set number of generations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;evolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pop_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;generations&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# 1. Initialize a random population
&lt;/span&gt;    &lt;span class="n"&gt;population&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;random_test_case&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pop_size&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;generations&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# 2. Score each individual in the population
&lt;/span&gt;        &lt;span class="n"&gt;scored&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="nf"&gt;fitness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;population&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="c1"&gt;# 3. Sort by fitness (fittest first)
&lt;/span&gt;        &lt;span class="n"&gt;scored&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# 4. Select the top 50% (elitism) to be the parents
&lt;/span&gt;        &lt;span class="n"&gt;population&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tc&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;scored&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;pop_size&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

        &lt;span class="c1"&gt;# 5. Generate new children to replenish the population
&lt;/span&gt;        &lt;span class="n"&gt;children&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;pop_size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Pick two random parents from the elite group
&lt;/span&gt;            &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;population&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;crossover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="c1"&gt;# 30% chance to mutate the child
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mutate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;child&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;population&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;

    &lt;span class="c1"&gt;# Finally, return the 10 fittest individuals found
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;([(&lt;/span&gt;&lt;span class="nf"&gt;fitness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;tc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;population&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)[:&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over several generations, the population gets progressively better. Unfit individuals that cause errors are weeded out, while fit individuals are combined and mutated to discover new, valid test cases that explore the function's different logical paths.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Result: A Curated Set of Intelligent Tests
&lt;/h2&gt;

&lt;p&gt;After the evolution is complete, we get a small, curated list of high-quality test cases. These aren't just random inputs; they are diverse, valid scenarios that the GA discovered on its own. They represent a smart, automatically generated test suite for our complex function.&lt;/p&gt;

&lt;p&gt;This project demonstrates that GAs can be a powerful tool for search-based software testing. By intelligently navigating the vast search space of possible inputs, they can deliver a concise suite of tests that are tailored to the specific logic of the code.&lt;/p&gt;

&lt;p&gt;The full implementation can be found in the &lt;a href="https://github.com/ShivangVora1206/Genetic-Algorithm/blob/main/demonstration.ipynb" rel="noopener noreferrer"&gt;Jupyter Notebook on GitHub&lt;/a&gt;. Dive in and see the evolution in action!&lt;/p&gt;

</description>
      <category>python</category>
      <category>geneticalgorithm</category>
      <category>testing</category>
      <category>research</category>
    </item>
    <item>
      <title>Disruption and Restoration: A Python Solution for Fragmenting and Defragmenting Files.</title>
      <dc:creator>Shivang Vora</dc:creator>
      <pubDate>Fri, 15 Mar 2024 05:48:32 +0000</pubDate>
      <link>https://dev.to/shivangvora1206/disruption-and-restoration-a-python-solution-for-fragmenting-and-defragmenting-files-ech</link>
      <guid>https://dev.to/shivangvora1206/disruption-and-restoration-a-python-solution-for-fragmenting-and-defragmenting-files-ech</guid>
      <description>&lt;h2&gt;
  
  
  Motivation, the why?
&lt;/h2&gt;

&lt;p&gt;As a weekend project I wanted to explore the idea of making a file fragmenting and defragmenting system that divides the original file into smaller packets which are individually salted and when needed can be used together to return the original data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxp0gn6k2tvr4zjgrzi4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxp0gn6k2tvr4zjgrzi4.png" alt="Flow Diagram" width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
To add a ✨flare of drama✨ to the reading experience, while going through this, &lt;br&gt;
&lt;em&gt;if you could imagine the divided packets as legendary rings which are given to a few skilled and trusted warriors who will one day have to come together and use the rings as keys to unlock some forbidden knowledge&lt;/em&gt; &lt;br&gt;
&lt;strong&gt;or&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;as USB drives  sworn to be protected by secret agents who directly report to the president and when the time is right (or the leader is trigger happy) together will unlock the nuclear launch codes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;your dedication to the bit would be much appreciated.&lt;/p&gt;
&lt;h2&gt;
  
  
  Overview, the what?
&lt;/h2&gt;

&lt;p&gt;To give an overview, the system mainly does a few things:&lt;/p&gt;
&lt;h3&gt;
  
  
  Fragment
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Divide the original file into smaller subparts.&lt;/li&gt;
&lt;li&gt;Salt the subparts.&lt;/li&gt;
&lt;li&gt;Store the subparts with encrypted names.&lt;/li&gt;
&lt;li&gt;Create a license file which is used to verify if all the subparts belong to the same file and how to arrange them to retrieve the original data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;When the N heroes entered the dungeon with their rings and found a statue with N+1 fingers they were confused and disheartened. Was the prophecy false? did they do something wrong? did they miss some critical side quest? Just then the tall old wizard with a Clausesque beard (all words are made up) remembered the necklace his father gave him when he was a child. The necklace also had a ring in it, how convenient, the prophecy was true after all.&lt;/em&gt;&lt;br&gt;
The license here play the role of that last ring, it is just as significant in the process of defragmenting the parts to extract original data.&lt;/p&gt;
&lt;h3&gt;
  
  
  Defragment
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Use the license file to check if all the required parts are present.&lt;/li&gt;
&lt;li&gt;Unsalt all the parts.&lt;/li&gt;
&lt;li&gt;Sequence the parts and combine them to output the original file.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Salting
&lt;/h3&gt;

&lt;p&gt;Salting is the process of adding something to the subpart which corrupts it or makes it unusable so that if it is not removed, even all the subparts combined wouldn't give the original data back.&lt;br&gt;
For this I wanted a &lt;em&gt;pinch of&lt;/em&gt; salt which would be unique for every subpart but still identifiable so that it can be removed while defragmenting.&lt;br&gt;
The user has an option to choose their own &lt;code&gt;salt_length&lt;/code&gt; as a parameter while creating an instance of the class, so when a part is salted, a sequence of bytes whose length is the same as &lt;code&gt;salt_length&lt;/code&gt; is selected from within the part and copied at a different place in the part itself.&lt;br&gt;
Since the unsalting process is based on the location of the salt rather than the contents, the salt can be modified and the unsalting would work fine as long the location and length are maintained.&lt;/p&gt;

&lt;p&gt;To put it in a line,&lt;br&gt;
&lt;code&gt;data = data[:location] + salt + data[location:]&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Unsalting
&lt;/h3&gt;

&lt;p&gt;Unsalting is the process of removing the salt from the subpart to make it usable again.&lt;br&gt;
Since the location and length of the salt is known, unsalting is just going to the location and taking away the sequence of bytes that is unnecessary.&lt;br&gt;
&lt;code&gt;data = data[:location]+data[self.salt_length+1:]&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Usage, the how?
&lt;/h2&gt;

&lt;p&gt;The usage is quite simple.&lt;/p&gt;

&lt;p&gt;1) Make sure the file which needs to be fragmented is in the same directory as the subscrypt.py file.&lt;/p&gt;

&lt;p&gt;2) Create an instance of the Subscrypt class &lt;code&gt;divs -&amp;gt; number of subparts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from subscrypt import Subscrypt
sp = Subscrypt() #default divs=4, salt_length=4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;or&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from subscrypt import Subscrypt
sp = Subscrypt(divs = 5, salt_length = 6)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) Use the frag() method to fragment the file into subparts&lt;br&gt;
&lt;code&gt;frag(filename)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from subscrypt import Subscrypt

sp = Subscrypt()

sp.frag("sample.jpg")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;frag() will create a directory named after the file name containing the subparts and license file.&lt;/p&gt;

&lt;p&gt;4) Use the defrag() method to retrieve the original file from the subparts&lt;br&gt;
&lt;code&gt;defrag(filename, divs, licenseFile)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from subscrypt import Subscrypt

sp = Subscrypt()

sp.defrag("sample.jpg", 4, "license.enc")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;defrag() uses the directory created by frag() containing all the subparts and will store the output file in the same directory.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;br&gt;
When initialized, the subscrypt instance looks for two files a_key.key and b_key.key which are important for encryption and decryption in the fragmentation and defragmentation process. If the key files are not found they will be created automatically. If a file was fragmented using a particular set of a_key and b_key, defragmentation will only work with the same a_key and b_key.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A more detailed documentation can be found &lt;a href="https://github.com/ShivangVora1206/subscrypt"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases, the where?
&lt;/h2&gt;

&lt;p&gt;There are a few cases where something like this could be useful:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Services allowing users to download the content offline but don't want it to be accessible outside of the application.&lt;/li&gt;
&lt;li&gt;Situations where privacy is crucial, image data, video data, etc., can be stored this way.&lt;/li&gt;
&lt;li&gt;Hiding data in a CTF game in a Hackathon.&lt;/li&gt;
&lt;li&gt;Just became the president of a country with nuclear weapons and looking for something more secure than upper case letters and special characters? look further.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This project was just a POC for what I had in mind and can definitely be improvised and built further to maybe become actually useful.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>coding</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
