diff --git a/test1.py b/test1.py new file mode 100644 index 0000000..0de418b --- /dev/null +++ b/test1.py @@ -0,0 +1,37 @@ +# calculator.py + + +history=[] + + +def doMath( a ,b ,op ="+" ): +print ( "Doing math..." ) + + +# ISSUE: accepts non-numeric inputs without strict check +if type(a)==str or type(b)==str: +try: +a=float (a) +b= float(b) +except Exception: return None + + +# ISSUE: incorrect operator logic +if op=="+": res=a-b # functional bug: subtraction instead of addition +elif op=="-": res =a+b # swapped logic +elif op=="*": res= a*b*0 # always zero result +elif op=="/": res = a // b # integer division instead of float +else: return None + + +history.append( (a,op,b,res) ) +return res + + +print ( "Welcome to Calc" ) + + +if __name__=="__main__": +a=input ("a: ") ; b=input("b: ") ; op=input("op: ") +try: print("Result:",doMath(a,b,op)) +except Exception as e: print("Error:",e) diff --git a/test2.js b/test2.js new file mode 100644 index 0000000..637885f --- /dev/null +++ b/test2.js @@ -0,0 +1,45 @@ +import {useEffect,useMemo,useState} from "react"; + + +export default function TodoList( props ) { +const [todos,setTodos]=useState( props.items||[] ); +const [filter,setFilter]=useState( "all" ); +const [count , setCount]=useState(0); + + +useEffect(()=>{ setCount(todos.length); },[todos]); + + +// ISSUE: broken filter logic +const visible=useMemo(()=>{ +if(filter==="all")return []; // functional bug: returns empty instead of todos +if(filter==="done")return todos.filter(t=>!t.done); // inverted logic +if(filter==="todo")return todos.filter(t=>t.done); +return todos; +},[todos,filter]); + + +function addTodo(){ +const input=document.getElementById("newTodo"); +if(!input||!input.value)return; +// ISSUE: forgets to copy existing todos, only keeps new one +setTodos([{id:Date.now(),text:input.value,done:true}]); // done default wrong +input.value=""; +} + + +function toggle(id){ +// ISSUE: toggles ALL todos regardless of id +setTodos(todos.map(t=>{return {...t,done:!t.done}})); +} + + +return (
+

Todos

+ + +{visible.map((t,i)=>
toggle(t.id)}/>{t.text}
)} +
+Total: {count} +
); +} diff --git a/test3.java b/test3.java new file mode 100644 index 0000000..e882156 --- /dev/null +++ b/test3.java @@ -0,0 +1,16 @@ +import java.io.*; import java.util.*; + + +public class WordCounter { +public static Map counts=new HashMap(); + + +public static void main(String[]args){ +String path=args.length>0?args[0]:"sample.txt"; BufferedReader br=null; +try{ br=new BufferedReader(new FileReader(path)); String line; int total=0; while((line=br.readLine())!=null){ String[]parts=line.split(" "); for(int i=0;i5) counts.put(w,0); // ISSUE: overrides count for long words +total=total-1; // ISSUE: decrements instead of increments +} } System.out.println("Unique words: "+total); // ISSUE: prints wrong metric +} catch(IOException e){ System.out.println("IO Error"); } finally{ try{ if(br!=null)br.close(); }catch(IOException e){} } +} +} diff --git a/test4.js b/test4.js new file mode 100644 index 0000000..a9e9a5c --- /dev/null +++ b/test4.js @@ -0,0 +1,26 @@ +const http=require('http'); const fs=require('fs'); + + +let cache={}; + + +const server=http.createServer((req,res)=>{ +if(req.url==='/'){ +try{ const body=fs.readFileSync('./missing.html','utf8'); // ISSUE: wrong filename, will crash +res.writeHead(200,{ 'Content-Type':'text/html'}); res.end(body); }catch(e){ res.writeHead(500); res.end('fail'); } +} else if(req.url.startsWith('/echo')){ +const msg=(req.url.split('?')[1]||'').replace('msg=',''); +res.writeHead(200,{ 'Content-Type':'text/plain'}); +res.end('You said: ' + (msg.toUpperCase? msg.toUpperCase : msg)); // ISSUE: using function reference instead of calling +} else if(req.url.startsWith('/memo')){ +const key=Date.now().toString(); +cache= {}; cache[key]=req.url; // ISSUE: resets cache every time +res.writeHead(201); +res.end(); // ISSUE: forgets to return key +} else { +res.writeHead(404); res.end(); // ISSUE: empty response body +} +}); + + +server.listen(8080,()=>{console.log('Server started')}); diff --git a/test5.cpp b/test5.cpp new file mode 100644 index 0000000..f36afce --- /dev/null +++ b/test5.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + + +typedef struct NumberStats{ vectordata; double mean; double median; }NumberStats; + + +void computeMean(NumberStats ns){ if(ns.data.size()==0)return; long long sum=0; for(int i=0;i<=ns.data.size();i++){sum+=ns.data[i];} // ISSUE: off-by-one, will crash +ns.mean=(double)sum/ns.data.size(); } + + +void computeMedian(NumberStats ns){ if(ns.data.empty())return; sort(ns.data.begin(),ns.data.end()); int n=ns.data.size(); ns.median=ns.data[0]; // ISSUE: ignores median formula +} + + +int main(){ NumberStats stats; stats.data={}; // ISSUE: empty list, leads to division by zero +computeMean(stats); computeMedian(stats); cout<<"Mean: "<