Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2.3: Structural Testing (Part 2)

Lesson 9 of 16 in the free Software Testing notes on Siksha Sarovar, written by Rohit Jangra.

Unit 2.3: Metrics & Complexity Analysis

1. Cyclomatic Complexity (McCabe's Metric)

Definition: Cyclomatic Complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code.

Why is it important?

  • It tells the tester exactly how many test cases are needed to achieve 100% Branch Coverage.
  • It helps developers identify code that is too complex and needs refactoring.

Detailed Calculation Methods:

Method 1: The Graph Formula V(G) = E - N + 2P

  • E: Number of Edges (Connecting lines).
  • N: Number of Nodes (States/Circles).
  • P: Connected components (Usually 1).

Method 2: The Decision Formula (Easiest) V(G) = P + 1

  • P: Number of "Predicate Nodes" (Decisions).
  • Hint: Just count the number of if, while, for, case statements and add 1.

Example Calculation:

      public void check(int i) {
          if (i > 10)         // Decision 1
             print("A");
          else if (i < 5)     // Decision 2
             print("B");
          else
             print("C");
      }
  • Number of Predicates (if) = 2.
  • Complexity = 2 + 1 = 3.
  • Meaning: You need 3 test cases to fully test this function.

Interpretation of Values:

  • 1 - 10: Structured and well-written. Low risk.
  • 11 - 20: Moderate complexity. Moderate risk.
  • 21 - 50: High complexity. High probability of bugs.
  • > 50: Unstable and unmaintainable. Rewrite required.

---

2. Graph Metrics

Beside Cyclomatic Complexity, other metrics help analyze the Control Flow Graph (CFG):

  • Size Metric: Just counting Lines of Code (LOC).
  • Edge/Node Ratio: A dense graph means convoluted logic ("Spaghetti Code").
  • Depth of Nesting: How many if inside if inside if? Too deep nesting is hard to read and test.

---

3. Data Flow Testing

Definition: A testing technique that focuses on the variables rather than the control logic. It tracks the lifecycle of data.

The Lifecycle of a Variable:

  1. Defined (d): The variable is initialized or assigned a value (e.g., x = 5).
  2. Used (u): The variable's value is read.
  3. Killed (k): The variable is destroyed or goes out of scope.

Common Data Flow Anomalies (Bugs):

  • Use-Before-Define: Using a variable that hasn't been initialized (leads to NullPointer or Garbage value).
  • Define-Kill: Assigning a value to a variable but never using it before it's destroyed (Useless code/Dead store).
  • Define-Define: Examining cases where a variable is assigned a value twice in a row without being used (First assignment was useless).