Skip to content

Commit 4e3116b

Browse files
Merge pull request #2509 from SidMehta/Test
Update backend.py
2 parents f18ed72 + 4b0cff7 commit 4e3116b

File tree

2 files changed

+134
-22
lines changed

2 files changed

+134
-22
lines changed

Diff for: BrowserHistory/backend.py

+43-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class DLL:
22
"""
33
a doubly linked list that holds the current page,
44
next page, and previous page.
5-
Used to enforce order in operations
5+
Used to enforce order in operations.
66
"""
77
def __init__(self, val: str =None):
88
self.val = val
@@ -14,61 +14,82 @@ class BrowserHistory:
1414
"""
1515
This class designs the operations of a browser history
1616
17-
It works by using a doubly linked list to hold the urls
17+
It works by using a doubly linked list to hold the urls with optimized
18+
navigation using step counters and memory management
1819
"""
1920

2021
def __init__(self, homepage: str):
2122
"""
2223
Returns - None
23-
Input - None
24+
Input - str
2425
----------
2526
- Initialize doubly linked list which will serve as the
2627
browser history and sets the current page
28+
- Initialize navigation counters
2729
"""
28-
self.head = DLL(homepage)
29-
self.curr = self.head
30+
self._head = DLL(homepage)
31+
self._curr = self._head
32+
self._back_count = 0
33+
self._forward_count = 0
3034

3135
def visit(self, url: str) -> None:
3236
"""
3337
Returns - None
3438
Input - str
3539
----------
3640
- Adds the current url to the DLL
37-
- sets both the next and previous values
41+
- Sets both the next and previous values
42+
- Cleans up forward history to prevent memory leaks
43+
- Resets forward count and increments back count
3844
"""
39-
url_node = DLL(url)
40-
self.curr.nxt = url_node
41-
url_node.prev = self.curr
45+
# Clear forward history to prevent memory leaks
46+
self._curr.nxt = None
47+
self._forward_count = 0
4248

43-
self.curr = url_node
49+
# Create and link new node
50+
url_node = DLL(url)
51+
self._curr.nxt = url_node
52+
url_node.prev = self._curr
4453

54+
# Update current node and counts
55+
self._curr = url_node
56+
self._back_count += 1
4557

4658
def back(self, steps: int) -> str:
4759
"""
4860
Returns - str
4961
Input - int
5062
----------
51-
- Iterates through the DLL backwards `step` number of times
52-
- returns the appropriate value
63+
- Moves backwards through history up to available steps
64+
- Updates navigation counters
65+
- Returns current page URL
5366
"""
54-
while steps > 0 and self.curr.prev:
55-
self.curr = self.curr.prev
67+
# Only traverse available nodes
68+
steps = min(steps, self._back_count)
69+
while steps > 0:
70+
self._curr = self._curr.prev
5671
steps -= 1
57-
return self.curr.val
58-
72+
self._back_count -= 1
73+
self._forward_count += 1
74+
return self._curr.val
5975

6076
def forward(self, steps: int) -> str:
6177
"""
6278
Returns - str
6379
Input - int
6480
----------
65-
- Iterates through the DLL forewards `step` number of times
66-
- returns the appropriate value
81+
- Moves forward through history up to available steps
82+
- Updates navigation counters
83+
- Returns current page URL
6784
"""
68-
while steps > 0 and self.curr.nxt:
69-
self.curr = self.curr.nxt
85+
# Only traverse available nodes
86+
steps = min(steps, self._forward_count)
87+
while steps > 0:
88+
self._curr = self._curr.nxt
7089
steps -= 1
71-
return self.curr.val
90+
self._forward_count -= 1
91+
self._back_count += 1
92+
return self._curr.val
7293

7394

7495
if __name__ == "__main__":
@@ -78,4 +99,4 @@ def forward(self, steps: int) -> str:
7899
param_3 = obj.forward(1)
79100

80101
print(param_2)
81-
print(param_3)
102+
print(param_3)

Diff for: BrowserHistory/tests/test_browser_history.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import unittest
2+
import sys
3+
import os
4+
5+
# Add parent directory to path to import backend
6+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
7+
from backend import BrowserHistory
8+
9+
class TestBrowserHistory(unittest.TestCase):
10+
def setUp(self):
11+
"""Set up test cases"""
12+
self.browser = BrowserHistory("homepage.com")
13+
14+
def test_initialization(self):
15+
"""Test proper initialization of BrowserHistory"""
16+
self.assertEqual(self.browser._curr.val, "homepage.com")
17+
self.assertEqual(self.browser._back_count, 0)
18+
self.assertEqual(self.browser._forward_count, 0)
19+
self.assertIsNone(self.browser._curr.nxt)
20+
self.assertIsNone(self.browser._curr.prev)
21+
22+
def test_visit(self):
23+
"""Test visit functionality and forward history cleanup"""
24+
self.browser.visit("page1.com")
25+
self.assertEqual(self.browser._curr.val, "page1.com")
26+
self.assertEqual(self.browser._back_count, 1)
27+
self.assertEqual(self.browser._forward_count, 0)
28+
29+
# Test forward history cleanup
30+
self.browser.visit("page2.com")
31+
self.browser.back(1)
32+
self.browser.visit("page3.com") # Should clear forward history
33+
self.assertIsNone(self.browser._curr.nxt)
34+
self.assertEqual(self.browser._forward_count, 0)
35+
36+
def test_back_navigation(self):
37+
"""Test back navigation with counter validation"""
38+
# Setup history
39+
self.browser.visit("page1.com")
40+
self.browser.visit("page2.com")
41+
42+
# Test normal back navigation
43+
result = self.browser.back(1)
44+
self.assertEqual(result, "page1.com")
45+
self.assertEqual(self.browser._back_count, 1)
46+
self.assertEqual(self.browser._forward_count, 1)
47+
48+
# Test back with more steps than available
49+
result = self.browser.back(5) # Should only go back 1 step
50+
self.assertEqual(result, "homepage.com")
51+
self.assertEqual(self.browser._back_count, 0)
52+
self.assertEqual(self.browser._forward_count, 2)
53+
54+
def test_forward_navigation(self):
55+
"""Test forward navigation with counter validation"""
56+
# Setup history and position
57+
self.browser.visit("page1.com")
58+
self.browser.visit("page2.com")
59+
self.browser.back(2) # Go back to homepage
60+
61+
# Test normal forward navigation
62+
result = self.browser.forward(1)
63+
self.assertEqual(result, "page1.com")
64+
self.assertEqual(self.browser._forward_count, 1)
65+
self.assertEqual(self.browser._back_count, 1)
66+
67+
# Test forward with more steps than available
68+
result = self.browser.forward(5) # Should only go forward remaining 1 step
69+
self.assertEqual(result, "page2.com")
70+
self.assertEqual(self.browser._forward_count, 0)
71+
self.assertEqual(self.browser._back_count, 2)
72+
73+
def test_complex_navigation(self):
74+
"""Test complex navigation patterns"""
75+
self.browser.visit("page1.com")
76+
self.browser.visit("page2.com")
77+
self.browser.visit("page3.com")
78+
79+
# Back navigation
80+
self.assertEqual(self.browser.back(2), "page1.com")
81+
82+
# New visit should clear forward history
83+
self.browser.visit("page4.com")
84+
self.assertEqual(self.browser._forward_count, 0)
85+
self.assertIsNone(self.browser._curr.nxt)
86+
87+
# Verify we can't go forward to cleared history
88+
self.assertEqual(self.browser.forward(1), "page4.com")
89+
90+
if __name__ == '__main__':
91+
unittest.main()

0 commit comments

Comments
 (0)