-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecursionTest.cs
More file actions
126 lines (110 loc) · 4.5 KB
/
RecursionTest.cs
File metadata and controls
126 lines (110 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Text;
using System.Collections.Generic;
namespace Platform.Exceptions.Tests
{
/// <summary>
/// Test program to compare recursive vs iterative implementations of BuildExceptionString
/// </summary>
public class RecursionTest
{
public static readonly string ExceptionContentsSeparator = "---";
static void Main()
{
// Create nested exceptions for testing
var innermost = new ArgumentException("Innermost exception");
var middle = new InvalidOperationException("Middle exception", innermost);
var outer = new Exception("Outer exception", middle);
Console.WriteLine("Testing recursive vs iterative implementations:");
Console.WriteLine();
// Test recursive version (current implementation)
var recursiveResult = ToStringWithAllInnerExceptionsRecursive(outer);
Console.WriteLine("RECURSIVE VERSION:");
Console.WriteLine(recursiveResult);
Console.WriteLine();
// Test iterative version
var iterativeResult = ToStringWithAllInnerExceptionsIterative(outer);
Console.WriteLine("ITERATIVE VERSION:");
Console.WriteLine(iterativeResult);
Console.WriteLine();
// Compare results
bool areEqual = recursiveResult == iterativeResult;
Console.WriteLine($"Results are equal: {areEqual}");
if (!areEqual)
{
Console.WriteLine("DIFFERENCE FOUND!");
Console.WriteLine($"Recursive length: {recursiveResult.Length}");
Console.WriteLine($"Iterative length: {iterativeResult.Length}");
}
}
// Current recursive implementation
public static string ToStringWithAllInnerExceptionsRecursive(Exception exception)
{
try
{
var sb = new StringBuilder();
BuildExceptionStringRecursive(sb, exception, 0);
return sb.ToString();
}
catch (Exception ex)
{
return "Unable to format exception.";
}
}
private static void BuildExceptionStringRecursive(StringBuilder sb, Exception exception, int level)
{
Indent(sb, level);
sb.AppendLine(exception.Message);
Indent(sb, level);
sb.AppendLine(ExceptionContentsSeparator);
if (exception.InnerException != null)
{
Indent(sb, level);
sb.AppendLine("Inner exception: ");
BuildExceptionStringRecursive(sb, exception.InnerException, level + 1);
}
Indent(sb, level);
sb.AppendLine(ExceptionContentsSeparator);
Indent(sb, level);
sb.AppendLine(exception.StackTrace);
}
// New iterative implementation
public static string ToStringWithAllInnerExceptionsIterative(Exception exception)
{
try
{
var sb = new StringBuilder();
BuildExceptionStringIterative(sb, exception);
return sb.ToString();
}
catch (Exception ex)
{
return "Unable to format exception.";
}
}
private static void BuildExceptionStringIterative(StringBuilder sb, Exception exception)
{
var stack = new Stack<(Exception ex, int level)>();
stack.Push((exception, 0));
while (stack.Count > 0)
{
var (current, level) = stack.Pop();
Indent(sb, level);
sb.AppendLine(current.Message);
Indent(sb, level);
sb.AppendLine(ExceptionContentsSeparator);
if (current.InnerException != null)
{
Indent(sb, level);
sb.AppendLine("Inner exception: ");
stack.Push((current.InnerException, level + 1));
}
Indent(sb, level);
sb.AppendLine(ExceptionContentsSeparator);
Indent(sb, level);
sb.AppendLine(current.StackTrace);
}
}
private static void Indent(StringBuilder sb, int level) => sb.Append('\t', level);
}
}