Spaces:
Running
Running
Download assignment2.html from sh1k4ku/python-playground: direct link, hf CLI and curl.
- Browser
- Download file 27 kB
-
https://huggingface.co/spaces/sh1k4ku/python-playground/resolve/8cc8d07cd76cb2fd2c79ef2526c2c6388b1f9fca/assignment2.html
- Command line
-
hf download hf://spaces/sh1k4ku/python-playground@8cc8d07cd76cb2fd2c79ef2526c2c6388b1f9fca/assignment2.html
-
curl -L -o assignment2.html https://huggingface.co/spaces/sh1k4ku/python-playground/resolve/8cc8d07cd76cb2fd2c79ef2526c2c6388b1f9fca/assignment2.html
27 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Assignment 2 β Software Testing</title> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css"> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/material-darker.min.css"> | |
| <style> | |
| :root { | |
| --bg:#1e1e2e; --panel:#252535; --card:#22222f; --border:#353550; | |
| --accent:#89b4fa; --accent-hover:#74a0f0; | |
| --text:#cdd6f4; --muted:#7f849c; --err:#f38ba8; --ok:#a6e3a1; --warn:#f9e2af; | |
| } | |
| * { box-sizing:border-box; } | |
| html,body { margin:0; } | |
| body { | |
| font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif; | |
| background:var(--bg); color:var(--text); line-height:1.55; font-size:14px; | |
| } | |
| .wrap { max-width:980px; margin:0 auto; padding:24px 28px 120px; } | |
| h1 { font-size:22px; margin:0 0 4px; } | |
| h2 { font-size:18px; margin:34px 0 6px; padding-top:14px; border-top:1px solid var(--border); } | |
| h2 .pts { font-size:12px; color:var(--muted); font-weight:500; } | |
| h3 { font-size:14px; color:var(--accent); margin:18px 0 6px; } | |
| p { margin:8px 0; } | |
| code, .mono { font-family:"SF Mono",Menlo,Consolas,monospace; } | |
| pre { background:#181825; border:1px solid var(--border); border-radius:8px; padding:12px 14px; overflow:auto; font-size:13px; } | |
| .muted { color:var(--muted); } | |
| a { color:var(--accent); } | |
| table.dt { border-collapse:collapse; font-size:12px; font-family:"SF Mono",Menlo,Consolas,monospace; margin:4px 0; } | |
| table.dt th, table.dt td { border:1px solid var(--border); padding:3px 8px; text-align:center; } | |
| table.dt th { background:var(--panel); color:var(--muted); font-weight:600; } | |
| table.spec { border-collapse:collapse; font-size:13px; margin:8px 0; } | |
| table.spec th, table.spec td { border:1px solid var(--border); padding:5px 10px; text-align:left; } | |
| table.spec th { background:var(--panel); } | |
| /* option card with radio */ | |
| .opt { | |
| border:1px solid var(--border); border-radius:10px; padding:10px 14px; margin:10px 0; | |
| background:var(--card); transition:border-color .12s, background .12s; | |
| } | |
| .opt.sel { border-color:var(--accent); background:#262a3f; } | |
| .opt-head { display:flex; align-items:center; gap:10px; cursor:pointer; font-weight:600; } | |
| .opt-head input { width:16px; height:16px; accent-color:var(--accent); cursor:pointer; } | |
| .opt-body { margin-top:8px; overflow:auto; } | |
| .opt-body img { max-width:100%; background:#fff; border-radius:6px; } | |
| /* fill-in blanks */ | |
| input.blank { | |
| width:64px; text-align:center; background:#181825; color:var(--ok); | |
| border:1px solid var(--border); border-radius:5px; padding:2px 4px; | |
| font-family:"SF Mono",Menlo,Consolas,monospace; font-size:13px; font-weight:600; | |
| } | |
| input.blank:focus { outline:none; border-color:var(--accent); } | |
| input.txt { | |
| background:#181825; color:var(--text); border:1px solid var(--border); | |
| border-radius:6px; padding:7px 10px; font-size:13px; | |
| font-family:"SF Mono",Menlo,Consolas,monospace; | |
| } | |
| input.txt:focus { outline:none; border-color:var(--accent); } | |
| .circ { display:inline-block; min-width:18px; color:var(--accent); font-weight:700; } | |
| button { | |
| background:var(--accent); color:#11111b; border:none; padding:8px 16px; | |
| border-radius:7px; font-size:13px; font-weight:700; cursor:pointer; transition:background .15s; | |
| } | |
| button:hover:not(:disabled){ background:var(--accent-hover); } | |
| button:disabled { opacity:.5; cursor:not-allowed; } | |
| button.ghost { background:transparent; color:var(--muted); border:1px solid var(--border); } | |
| button.ghost:hover:not(:disabled){ color:var(--text); } | |
| /* Q4 playground */ | |
| .pg { border:1px solid var(--border); border-radius:10px; overflow:hidden; margin:12px 0; background:var(--card); } | |
| .pg-head { display:flex; align-items:center; gap:10px; padding:8px 12px; background:var(--panel); border-bottom:1px solid var(--border); flex-wrap:wrap; } | |
| .pg-head .status { font-size:12px; color:var(--muted); margin-left:auto; } | |
| .pg-head .status.ready { color:var(--ok); } | |
| .pg-head .status.error { color:var(--err); } | |
| .CodeMirror { height:auto; min-height:230px; font-size:13px; font-family:"SF Mono",Menlo,Consolas,monospace; } | |
| .pg-row { display:flex; gap:14px; flex-wrap:wrap; align-items:flex-end; padding:10px 12px; border-top:1px solid var(--border); } | |
| .pg-row label { font-size:12px; color:var(--muted); display:block; margin-bottom:4px; } | |
| #q4out { margin:0; padding:12px 14px; border-top:1px solid var(--border); background:#181825; font-size:13px; white-space:pre-wrap; min-height:40px; } | |
| #q4out .ok{color:var(--ok);} #q4out .err{color:var(--err);} #q4out .warn{color:var(--warn);} #q4out .meta{color:var(--muted);} | |
| /* sticky export bar */ | |
| .exportbar { | |
| position:fixed; left:0; right:0; bottom:0; background:var(--panel); | |
| border-top:1px solid var(--border); padding:10px 28px; | |
| display:flex; align-items:center; gap:14px; z-index:10; | |
| } | |
| .exportbar .sid { display:flex; align-items:center; gap:8px; font-size:12px; color:var(--muted); } | |
| .exportbar .spacer { flex:1; } | |
| .toast { font-size:12px; color:var(--ok); } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="wrap"> | |
| <h1>Software Testing β Assignment 2</h1> | |
| <p class="muted">Answer all questions below. Q4 has an interactive playground. When you are done, export your answers as JSON at the bottom.</p> | |
| <!-- ============ Q1 ============ --> | |
| <h2>Q1 Extended Entry Decision Table <span class="pts">[6 pts] Β· single choice</span></h2> | |
| <p>Test the UNSW Lecture-Theatre Booking system with Decision-Table testing. Rules are checked one by one and the <b>first matching rule</b> applies. Outputs: <code>0</code>=Booked, <code>1</code>=Wait-list, <code>2</code>=Rejected.</p> | |
| <table class="spec"> | |
| <tr><th>Input</th><th>Encoding</th></tr> | |
| <tr><td>room_size</td><td>1=Small, 2=Medium, 3=Large</td></tr> | |
| <tr><td>time_slot</td><td>1=Morning, 2=Afternoon, 3=Evening</td></tr> | |
| <tr><td>week</td><td>1 to 12 (numeric)</td></tr> | |
| <tr><td>equipment</td><td>1=Standard, 2=AV, 3=ComputerLab, 4=Hybrid</td></tr> | |
| <tr><td>staff_role</td><td>1=Academic, 2=Admin, 3=Guest</td></tr> | |
| </table> | |
| <p class="muted">Rules: invalid input → rejected; exam weeks 11β12 allow only academics and block Large theatres; teaching weeks 1β10 have room/equipment/role constraints; otherwise booked. <b>Exactly one table below is correct β select it.</b></p> | |
| <div id="q1opts"></div> | |
| <!-- ============ Reference function ============ --> | |
| <h2>Reference function <span class="pts">for Q2, Q3, Q4</span></h2> | |
| <pre id="reffn"></pre> | |
| <!-- ============ Q2 ============ --> | |
| <h2>Q2 Control-Flow Graph <span class="pts">[3 pts] Β· single choice</span></h2> | |
| <p>Below are four candidate CFGs for <code>binary_search</code>, written as DOT graphs and rendered. Exactly one is correct β select it.</p> | |
| <div id="q2opts"></div> | |
| <!-- ============ Q3 ============ --> | |
| <h2>Q3 Loop-Boundary Test Cases <span class="pts">[3 pts] Β· fill in the blanks</span></h2> | |
| <p>Develop a test suite achieving loop-boundary coverage (loop runs 0, 1, and 2 times). Fill each circled blank. Recall <code>mid = int(round((top+bott)/2.0))</code>.</p> | |
| <p class="muted">Marking: each loop case = 1 mark; within a case, one wrong blank β0.5, two or more wrong β1.</p> | |
| <pre id="q3block"></pre> | |
| <!-- ============ Q4 ============ --> | |
| <h2>Q4 Fault-Based Testing: Plant a Bug and Catch It <span class="pts">[3 pts] Β· playground</span></h2> | |
| <p>(a) Introduce <b>exactly one</b> single-line bug into <code>binary_search</code> so that <b>all three Q3 tests still pass</b> (the bug is invisible to the Q3 suite). | |
| (b) Give <b>one</b> extra test case that <b>distinguishes</b> the mutant from the original (original passes, mutant fails).</p> | |
| <p class="muted">Use the playground: edit the function to your mutant, enter your distinguishing test, and press <b>Run check</b>. It reports whether your mutant still passes the Q3 suite and whether your test separates the two.</p> | |
| <p style="margin-top:14px"><b>(a) Describe your one-line mutation</b> (form <code>from → to</code>):</p> | |
| <input class="txt" id="q4mutation" style="width:100%" placeholder="e.g. top = mid - 1 -> top = mid - 2"> | |
| <div class="pg"> | |
| <div class="pg-head"> | |
| <b>Q4 Playground</b> | |
| <button id="q4run" disabled>βΆ Run check</button> | |
| <button id="q4reset" class="ghost">Reset to original</button> | |
| <span class="status" id="q4status">Loading Python runtimeβ¦</span> | |
| </div> | |
| <textarea id="q4code"></textarea> | |
| <div class="pg-row"> | |
| <div><label>(b) Distinguishing test β target (Python list)</label> | |
| <input class="txt" id="q4target" placeholder="[1, 3, 5, 7]" style="width:220px"></div> | |
| <div><label>key</label> | |
| <input class="txt" id="q4key" placeholder="3" style="width:90px"></div> | |
| </div> | |
| <pre id="q4out"><span class="meta">(edit the function, fill the test, then Run check)</span></pre> | |
| </div> | |
| <!-- ============ Q5 ============ --> | |
| <h2>Q5 Halstead Programming Effort <span class="pts">[1 pt] Β· single choice</span></h2> | |
| <p>Calculate the Halstead <b>programming effort</b> for the following code (parentheses are not counted as operators):</p> | |
| <pre id="q5code"></pre> | |
| <div id="q5opts"></div> | |
| </div> | |
| <!-- ============ sticky export bar ============ --> | |
| <div class="exportbar"> | |
| <div class="sid">Student ID <input class="txt" id="studentId" placeholder="z1234567" style="width:130px"></div> | |
| <div class="spacer"></div> | |
| <span class="toast" id="toast"></span> | |
| <button class="ghost" id="clearBtn">Clear saved</button> | |
| <button class="ghost" id="copyBtn">Copy JSON</button> | |
| <button id="exportBtn">β€ Export JSON</button> | |
| </div> | |
| <script src="https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/python/python.min.js"></script> | |
| <script> | |
| // ============ canonical reference function ============ | |
| const REF_SRC = `def binary_search(key: int, target: list[int]): | |
| size = len(target) | |
| bott = 0 | |
| top = size - 1 | |
| location = 0 | |
| found = False | |
| if target[location] == key: | |
| found = True | |
| else: | |
| found = False | |
| while bott <= top and not found: | |
| mid = int(round((top + bott) / 2.0)) | |
| if target[mid] == key: | |
| found = True | |
| location = mid | |
| else: | |
| if target[mid] < key: | |
| bott = mid + 1 | |
| else: | |
| top = mid - 1 | |
| return found, location`; | |
| document.getElementById("reffn").textContent = REF_SRC; | |
| // ============ Q1: six decision tables ============ | |
| const Q1_COLS = ["rule","room_size","time_slot","week","equipment","staff_role","outcome"]; | |
| const Q1 = { | |
| 1: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]], | |
| 2: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","2"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]], | |
| 3: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","-","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]], | |
| 4: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","10-12","-","2/3","2"],["B2","3","-","10-12","-","1","2"],["C1","3","3","1-9","-","-","2"],["C2","3","-","1-9","3","-","2"],["C3","3","-","1-9","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-9","3","3","1"],["F1","1","3","1-9","-","2","1"],["F2","1","-","10-12","-","2","N/A"],["G","-","-","-","-","-","0"]], | |
| 5: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]], | |
| 6: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]], | |
| }; | |
| function dtTable(rows){ | |
| let h = "<table class='dt'><tr>" + Q1_COLS.map(c=>`<th>${c}</th>`).join("") + "</tr>"; | |
| for (const r of rows) h += "<tr>" + r.map(x=>`<td>${x}</td>`).join("") + "</tr>"; | |
| return h + "</table>"; | |
| } | |
| const answers = { q1:null, q2:null, q3:{}, q5:null }; | |
| function buildChoice(host, name, items, onSel){ | |
| host.innerHTML = ""; | |
| for (const it of items){ | |
| const card = document.createElement("div"); card.className = "opt"; | |
| card.innerHTML = `<label class="opt-head"><input type="radio" name="${name}" value="${it.val}"><span>${it.label}</span></label><div class="opt-body">${it.body}</div>`; | |
| const radio = card.querySelector("input"); | |
| radio.onchange = () => { | |
| [...host.querySelectorAll(".opt")].forEach(o=>o.classList.remove("sel")); | |
| card.classList.add("sel"); onSel(it.val); | |
| }; | |
| host.appendChild(card); | |
| } | |
| } | |
| buildChoice(document.getElementById("q1opts"), "q1", | |
| [1,2,3,4,5,6].map(n=>({val:String(n), label:"Option "+n, body:dtTable(Q1[n])})), | |
| v => { answers.q1 = v; saveSession(); }); | |
| // ============ Q2: four CFG images ============ | |
| buildChoice(document.getElementById("q2opts"), "q2", | |
| [1,2,3,4].map(n=>({val:String(n), label:"Option "+n, body:`<img src="a2/assets/img${n-1}.png" alt="CFG option ${n}">`})), | |
| v => { answers.q2 = v; saveSession(); }); | |
| // ============ Q3: fill-in trace with 12 blanks ============ | |
| const Q3_TEXT = | |
| `Test 1 loop runs 0 times | |
| Input: target = [5, 8, 12], key = @1@ | |
| Output: found = @2@ , location = @3@ | |
| Reason: target[0] @4@ key, so found is True before the loop; | |
| 'not found' in the while condition is @5@ , the body is skipped. | |
| Test 2 loop runs 1 time | |
| Input: target = [2, 4, 6], key = 4 | |
| Output: found = True, location = @6@ | |
| Reason: target[0]=2 != 4, enter the loop. First mid = @7@ , | |
| target[mid] = @8@ = key, found=True, location set, loop exits. | |
| Test 3 loop runs 2 times | |
| Input: target = [1, 3, 5, 7, 9], key = @9@ | |
| Output: found = True, location = @10@ | |
| Reason: first mid=2, target[2]=5 < 9, bott becomes @11@ ; | |
| second mid = @12@ , where round(3.5)=4, target[4]=9 = key, found.`; | |
| (function buildQ3(){ | |
| const host = document.getElementById("q3block"); | |
| host.textContent = ""; host.style.whiteSpace = "pre-wrap"; | |
| const parts = Q3_TEXT.split(/@(\d+)@/); | |
| for (let i=0;i<parts.length;i++){ | |
| if (i % 2 === 0){ host.appendChild(document.createTextNode(parts[i])); } | |
| else { | |
| const n = parts[i]; | |
| const wrap = document.createElement("span"); | |
| const circ = document.createElement("span"); circ.className="circ"; circ.textContent = "βͺ".replace("βͺ","")+""; | |
| const inp = document.createElement("input"); | |
| inp.className = "blank"; inp.dataset.n = n; inp.title = "blank "+n; | |
| inp.placeholder = n; | |
| inp.oninput = () => { answers.q3[n] = inp.value; saveSession(); }; | |
| host.appendChild(inp); | |
| } | |
| } | |
| })(); | |
| // ============ Q5: code + options ============ | |
| document.getElementById("q5code").textContent = | |
| `d = a + b # addition | |
| e = b * c # multiplication | |
| f = d / (e + 1) # division and addition | |
| if f > 0: | |
| g = f + d - e # operations in conditional branch | |
| if f <= 0: | |
| g = d - e / (c + 1) # different operations without else | |
| result = g * a + b - c # final operation | |
| return result`; | |
| buildChoice(document.getElementById("q5opts"), "q5", | |
| [["a","(a) 2729.72"],["b","(b) 2827.21"],["c","(c) 2882.64"],["d","(d) 2938.08"]] | |
| .map(([v,l])=>({val:v, label:l, body:""})), | |
| v => { answers.q5 = v; saveSession(); }); | |
| // ============ Q4 playground (Pyodide) ============ | |
| const q4status = document.getElementById("q4status"); | |
| const q4run = document.getElementById("q4run"); | |
| const q4out = document.getElementById("q4out"); | |
| const q4target = document.getElementById("q4target"); | |
| const q4key = document.getElementById("q4key"); | |
| let lastQ4 = { original_output:"", mutant_output:"" }; | |
| const editor = CodeMirror.fromTextArea(document.getElementById("q4code"), { | |
| mode:"python", theme:"material-darker", lineNumbers:true, indentUnit:4, tabSize:4, | |
| extraKeys:{ "Cmd-Enter":runQ4, "Ctrl-Enter":runQ4, Tab:cm=>cm.replaceSelection(" ") }, | |
| }); | |
| editor.setValue(REF_SRC); | |
| document.getElementById("q4reset").onclick = () => editor.setValue(REF_SRC); | |
| editor.on("change", () => saveSession()); | |
| function q4append(t, cls){ const s=document.createElement("span"); if(cls)s.className=cls; s.textContent=t; q4out.appendChild(s); } | |
| const HARNESS = ` | |
| import json, ast | |
| def __make_orig(): | |
| ns = {} | |
| exec(__REF, ns) | |
| return ns["binary_search"] | |
| def __run(): | |
| res = {} | |
| try: | |
| orig = __make_orig() | |
| except Exception as e: | |
| return json.dumps({"error":"reference error: "+str(e)}) | |
| ns = {} | |
| try: | |
| exec(__MUT, ns) | |
| except Exception as e: | |
| return json.dumps({"error":"Your code has an error: "+str(e)}) | |
| mut = ns.get("binary_search") | |
| if mut is None: | |
| return json.dumps({"error":"Your code must define a function named binary_search."}) | |
| q3 = [([5,8,12],5), ([2,4,6],4), ([1,3,5,7,9],9)] | |
| rows = []; allpass = True | |
| for tgt,k in q3: | |
| try: o = orig(k, list(tgt)) | |
| except Exception as e: o = "ERR: "+str(e) | |
| try: m = mut(k, list(tgt)) | |
| except Exception as e: m = "ERR: "+str(e) | |
| same = (str(o)==str(m)) | |
| if not same: allpass = False | |
| rows.append({"target":str(list(tgt)),"key":k,"orig":str(o),"mut":str(m),"same":same}) | |
| dist = None | |
| ts = __TGT.strip(); ks = __KEY.strip() | |
| if ts and ks: | |
| try: | |
| tgt = ast.literal_eval(ts); k = ast.literal_eval(ks) | |
| try: o = orig(k, tgt) | |
| except Exception as e: o = "ERR: "+str(e) | |
| try: m = mut(k, tgt) | |
| except Exception as e: m = "ERR: "+str(e) | |
| dist = {"target":str(tgt),"key":str(k),"orig":str(o),"mut":str(m),"differs":(str(o)!=str(m))} | |
| except Exception as e: | |
| dist = {"error":"could not parse target/key: "+str(e)} | |
| return json.dumps({"rows":rows,"allpass":allpass,"dist":dist}) | |
| __run() | |
| `; | |
| let pyodide = null; | |
| async function runQ4(){ | |
| if (!pyodide || q4run.disabled) return; | |
| q4run.disabled = true; q4out.innerHTML = ""; | |
| try { | |
| pyodide.globals.set("__REF", REF_SRC); | |
| pyodide.globals.set("__MUT", editor.getValue()); | |
| pyodide.globals.set("__TGT", q4target.value || ""); | |
| pyodide.globals.set("__KEY", q4key.value || ""); | |
| const rep = JSON.parse(pyodide.runPython(HARNESS)); | |
| if (rep.error){ q4append(rep.error+"\n","err"); lastQ4={original_output:"",mutant_output:""}; return; } | |
| q4append("Q3 suite on your mutant (must all pass / match):\n","meta"); | |
| for (const r of rep.rows){ | |
| const tag = r.same ? " PASS " : " FAIL "; | |
| q4append(tag, r.same?"ok":"err"); | |
| q4append(`target=${r.target}, key=${r.key} original=${r.orig} mutant=${r.mut}\n`, "meta"); | |
| } | |
| if (rep.allpass) q4append("(a) β Mutant is invisible to the Q3 suite β all three match.\n","ok"); | |
| else q4append("(a) β Mutant changes a Q3 result β the Q3 suite would catch it. Pick a bug on an unexercised path.\n","warn"); | |
| q4append("\n"); | |
| if (!rep.dist){ q4append("(b) Fill target + key above to test your distinguishing case.\n","meta"); lastQ4={original_output:"",mutant_output:""}; } | |
| else if (rep.dist.error){ q4append("(b) "+rep.dist.error+"\n","err"); lastQ4={original_output:"",mutant_output:""}; } | |
| else { | |
| lastQ4 = { original_output: rep.dist.orig, mutant_output: rep.dist.mut }; | |
| q4append(`(b) Distinguishing test target=${rep.dist.target}, key=${rep.dist.key}\n`,"meta"); | |
| q4append(` original = ${rep.dist.orig}\n mutant = ${rep.dist.mut}\n`,"meta"); | |
| if (rep.dist.differs) q4append(" β This test distinguishes the mutant from the original.\n","ok"); | |
| else q4append(" β Same output on both β this test does NOT distinguish them yet.\n","warn"); | |
| } | |
| } catch(e){ q4append("Runtime error: "+e+"\n","err"); } | |
| finally { q4run.disabled = false; saveSession(); } | |
| } | |
| q4run.onclick = runQ4; | |
| (async () => { | |
| try { | |
| pyodide = await loadPyodide({}); | |
| q4status.textContent = "β Ready"; q4status.className = "status ready"; q4run.disabled = false; | |
| } catch(e){ q4status.textContent = "Failed to load"; q4status.className = "status error"; } | |
| })(); | |
| // ============ JSON export (fixed format) ============ | |
| function buildJSON(){ | |
| const q3 = {}; | |
| for (let i=1;i<=12;i++) q3[i] = (answers.q3[i] || ""); | |
| return { | |
| assignment: "Software Testing β Assignment 2", | |
| student_id: document.getElementById("studentId").value.trim(), | |
| answers: { | |
| q1: answers.q1 || "", | |
| q2: answers.q2 || "", | |
| q3: q3, | |
| q4: { | |
| mutation: document.getElementById("q4mutation").value.trim(), | |
| mutant_code: editor.getValue(), | |
| test: { target: q4target.value.trim(), key: q4key.value.trim() }, | |
| original_output: lastQ4.original_output, | |
| mutant_output: lastQ4.mutant_output | |
| }, | |
| q5: answers.q5 || "" | |
| } | |
| }; | |
| } | |
| function toast(msg){ const t=document.getElementById("toast"); t.textContent=msg; setTimeout(()=>t.textContent="",2500); } | |
| document.getElementById("exportBtn").onclick = () => { | |
| const data = JSON.stringify(buildJSON(), null, 2); | |
| const blob = new Blob([data], {type:"application/json"}); | |
| const a = document.createElement("a"); | |
| const sid = document.getElementById("studentId").value.trim() || "submission"; | |
| a.href = URL.createObjectURL(blob); a.download = `assignment2_${sid}.json`; | |
| a.click(); URL.revokeObjectURL(a.href); toast("Exported β"); | |
| }; | |
| document.getElementById("copyBtn").onclick = async () => { | |
| try { await navigator.clipboard.writeText(JSON.stringify(buildJSON(), null, 2)); toast("Copied β"); } | |
| catch(_){ toast("Copy failed"); } | |
| }; | |
| // ============ 24h local session (auto-save / restore) ============ | |
| const SESSION_KEY = "st_a2_session_v1"; | |
| const SESSION_TTL = 24 * 3600 * 1000; // 24 hours | |
| let __saveTimer = null; | |
| function saveSession(){ | |
| // debounce rapid edits (typing in the editor / blanks) | |
| clearTimeout(__saveTimer); | |
| __saveTimer = setTimeout(() => { | |
| try { | |
| const d = { | |
| ts: Date.now(), | |
| studentId: document.getElementById("studentId").value, | |
| q1: answers.q1, q2: answers.q2, q5: answers.q5, | |
| q3: answers.q3, | |
| q4: { | |
| mutation: document.getElementById("q4mutation").value, | |
| code: editor.getValue(), | |
| target: q4target.value, key: q4key.value, | |
| original_output: lastQ4.original_output, mutant_output: lastQ4.mutant_output | |
| } | |
| }; | |
| localStorage.setItem(SESSION_KEY, JSON.stringify(d)); | |
| } catch(_){} | |
| }, 250); | |
| } | |
| function restoreRadio(name, val){ | |
| if (!val) return; | |
| const r = document.querySelector(`input[name="${name}"][value="${val}"]`); | |
| if (r){ r.checked = true; const opt = r.closest(".opt"); if (opt) opt.classList.add("sel"); answers[name] = val; } | |
| } | |
| function loadSession(){ | |
| let d; | |
| try { d = JSON.parse(localStorage.getItem(SESSION_KEY)); } catch(_){ return; } | |
| if (!d || !d.ts || (Date.now() - d.ts) > SESSION_TTL){ localStorage.removeItem(SESSION_KEY); return; } | |
| document.getElementById("studentId").value = d.studentId || ""; | |
| restoreRadio("q1", d.q1); restoreRadio("q2", d.q2); restoreRadio("q5", d.q5); | |
| if (d.q3){ answers.q3 = Object.assign({}, d.q3); | |
| document.querySelectorAll("input.blank").forEach(inp => { const v = d.q3[inp.dataset.n]; if (v != null) inp.value = v; }); } | |
| if (d.q4){ | |
| document.getElementById("q4mutation").value = d.q4.mutation || ""; | |
| if (d.q4.code) editor.setValue(d.q4.code); | |
| q4target.value = d.q4.target || ""; q4key.value = d.q4.key || ""; | |
| lastQ4 = { original_output: d.q4.original_output || "", mutant_output: d.q4.mutant_output || "" }; | |
| } | |
| // banner | |
| const mins = Math.round((Date.now() - d.ts) / 60000); | |
| const ago = mins < 60 ? mins + " min ago" : Math.round(mins/60) + " h ago"; | |
| const b = document.createElement("div"); | |
| b.style.cssText = "margin:10px 0;padding:8px 12px;border:1px solid var(--border);border-radius:8px;background:#262a3f;font-size:12px;color:var(--muted)"; | |
| b.innerHTML = `βΊ Restored your answers from this browser (saved ${ago}). They are kept for 24 hours. `; | |
| const x = document.createElement("button"); x.textContent = "Clear & start fresh"; x.className = "ghost"; | |
| x.style.cssText = "padding:3px 10px;font-size:11px;margin-left:6px"; | |
| x.onclick = clearSession; | |
| b.appendChild(x); | |
| const wrap = document.querySelector(".wrap"); | |
| wrap.insertBefore(b, wrap.children[2]); | |
| } | |
| function clearSession(){ localStorage.removeItem(SESSION_KEY); location.reload(); } | |
| // save on the free-text fields too | |
| ["studentId","q4mutation"].forEach(id => document.getElementById(id).addEventListener("input", saveSession)); | |
| [q4target, q4key].forEach(el => el.addEventListener("input", saveSession)); | |
| document.getElementById("clearBtn").onclick = clearSession; | |
| // restore once everything above is defined | |
| loadSession(); | |
| </script> | |
| </body> | |
| </html> | |