0

I was to make a knapsack program that could take in numbers and store their counts as well as have a UI like interface the user could put commands into such as saving and loading from a file. While all of this works perfectly and the code for such is provided below I'm having difficulty producing an expected output of a testcase for this program.

I am wondering what I'm doing wrong and why to stdout there are no "> "being produced. If i remove the line of fprintf(stdout, "> "); from the else portion then there are 3 "> " printed to stdout. I need to be able to print 4 of them but I'm stumped on how this is possible. I'm using travis-ci.com to test this test case and this is where its showing me the error of it failing. Full code and testcase below.

Current Output

    Your stderr:
Error: Bad item "abc"
Error: Bad item "xyz"
unknown command: d

Desired Output

    Expected stdout:
> > > > 
    Expected stderr:
Error: Bad item "abc"
Error: Bad item "xyz"
Unknown command: d

knapsack-testcase4.sh

#!/bin/bash

echo "Test case 4"
echo "Bad commands"
echo "a abc"
echo "r xyz"
echo "d"
echo -e "Error: Bad item \"abc\"\nError: Bad item \"xyz\"\nUnknown command: d" > expectedstderr.tmp
echo -ne "> > > > " > expectedstdout.tmp
echo -e "a abc\nr xyz\nd" | ./knapsack-shell 1>actualstdout.tmp 2>actualstderr.tmp &
sleep 1
killall -9 knapsack-shell

echo "  Expected stdout:"
cat expectedstdout.tmp
echo ""
echo "  Expected stderr:"
cat expectedstderr.tmp

stdoutres=`diff expectedstdout.tmp actualstdout.tmp | wc -l`
stderrres=`diff expectedstderr.tmp actualstderr.tmp | wc -l`
if [ $stdoutres -eq 0 ] && [ $stderrres -eq 0 ]; then
    echo "Test passed"
else 
    if [ $stdoutres -ne 0 ]; then
        echo "  Your stdout:"
        cat actualstdout.tmp
        echo ""
    fi
    if [ $stderrres -ne 0 ]; then
        echo "  Your stderr:"
        cat actualstderr.tmp
    fi
    rm *.tmp
    exit 1
fi
rm *.tmp

knapsack-shell.c

#include <stdio.h>
#include "knapsack.c"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFFER_SIZE 1000
void removeSpaces(char *str1)
{
    char *str2;
    str2=str1;
    while (*str2==' ') str2++;
    if (str2!=str1) memmove(str1,str2,strlen(str2)+1);
}

int main()
{
    listitemptr k2 = NULL;
    char input[100];
    char command;
    int i, returnval = 0;
    char filename[250];
    char userfile[260];
    char buffer[BUFFER_SIZE];
    char *error;
    int totalRead = 0;
    fprintf(stdout, "> ");  
   for (;;) {
       FILE* pf = NULL;

        if (!fgets(input, sizeof input, stdin))
            break;
        i = strspn(input, " \t\n");  /* skip blanks */
        command = input[i++];
        if (command == 'q' && input[i] == '\n')
            break;
        if (command == 'p') {
            KnapsackPrint(&k2);
    fprintf(stdout, "> ");
            continue;
        }
        if (command == 'a') {
            int item;
            if (sscanf(input + i, "%i", &item) != 1) {
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");  
                fprintf(stderr, "Error: Bad item \"%s\"\n", strtok(input + i, "\n"));

                continue;
            }
            KnapsackAdd(&k2, item);
            KnapsackPrint(&k2);
    fprintf(stdout, "> ");
            continue;
        }
        if (command == 'r') {

            int item;
            if (sscanf(input + i, "%i", &item) != 1) {
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");
                fprintf(stderr, "Error: Bad item \"%s\"\n", strtok(input + i, "\n"));
                continue;
            }

            if(KnapsackRemove(&k2, item) == -1){
                fprintf(stderr, "Error: item %d does not exist in knapsack\n", item);
    fprintf(stdout, "> ");
                continue;
            }else{
            KnapsackPrint(&k2);
        fprintf(stdout, "> ");
            continue;
            }

        }
        if(command == 's'){
            if(( pf = fopen(input + i, "a")) != NULL){
                error = input + i;
                removeSpaces(error);
                KnapsackPrint2(&k2, pf);
                fprintf(stdout, "stored in file \"%s\"\n", strtok(input+i, "\n"));
    fprintf(stdout, "> ");
            }else{
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");
                fprintf(stdout, "Error: unable to save file \"%s\"\n", strtok(input+i, "\n"));
            }
            continue;
        }
        if(command == 'l'){
            if((pf = fopen(input + i, "r")) != NULL){
                error = input + i;
                removeSpaces(error);
                fprintf(stdout, "loaded from file \"%s\"\n", strtok(input+i, "\n"));
                fprintf(stdout, "knapsack:");
                while(fgets(buffer, BUFFER_SIZE, pf) != NULL){
                    totalRead = strlen(buffer);

                    buffer[totalRead -1] = buffer[totalRead -1] == '\n' ? '\0' : buffer[totalRead -1];

                    printf("%s\n", buffer);
            fprintf(stdout, "> ");
                }

            }else {
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");
                fprintf(stderr, "Error: unable to load file \"%s\"\n", strtok(input+i, "\n"));

            }
            continue;
        }
        else{

        fprintf(stderr, "unknown command: %s", input);
    fprintf(stdout, "> ");          
        }


     }
    return returnval;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Bret Hasel
  • 303
  • 1
  • 11

0 Answers0