Skip to content
Snippets Groups Projects
Commit 337f0f72 authored by dtt47's avatar dtt47
Browse files

assignment 2

parent 8f0864a2
No related branches found
No related tags found
No related merge requests found
A2/db.h 0 → 100644
#ifndef __DB_H__
#define __DB_H__
// Basic student database record. Note:
// 1. id must be > 0. A student id==0 means the record has been deleted
// 2. gpa is an int, should be between 0<=gpa<=500, real gpa is gpa/100.0 this
// simplifies dealing with floating point types
// 3. Notice that the student struct was engineered to have a size of
// 64 bytes. There are reasons for using such a number
typedef struct student
{
int id;
char fname[24];
char lname[32];
int gpa;
} student_t;
// Define limits for sudent ids and allowable GPA ranges. Note GPA values will
// be stored as integers but printed as floats. For example a GPA of 450 is really
// that value divided by 100.0 or 4.50.
#define MIN_STD_ID 1
#define MAX_STD_ID 100000
#define MIN_STD_GPA 0
#define MAX_STD_GPA 500
// some useful constants you should consider using versus hard coding
// in your program.
static const student_t EMPTY_STUDENT_RECORD = {0};
static const int STUDENT_RECORD_SIZE = sizeof(struct student);
static const int DELETED_STUDENT_ID = 0;
#define DB_FILE "student.db" // name of database file
#define TMP_DB_FILE ".tmp_student.db" // for extra credit
#endif
\ No newline at end of file
# Compiler settings
CC = gcc
CFLAGS = -Wall -Wextra -g
# Target executable name
TARGET = sdbsc
# Find all source and header files
SRCS = $(wildcard *.c)
HDRS = $(wildcard *.h)
# Default target
all: $(TARGET)
# Compile source to executable
$(TARGET): $(SRCS) $(HDRS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS)
# Clean up build files
clean:
rm -f $(TARGET)
rm -f student.db
test:
./test.sh
# Phony targets
.PHONY: all clean
\ No newline at end of file
This diff is collapsed.
#ifndef __SDB_H__
#include "db.h" //get student record type
// prototypes for functions go below for this assignment
int open_db(char *dbFile, bool should_truncate);
int add_student(int fd, int id, char *fname, char *lname, int gpa);
int get_student(int fd, int id, student_t *s);
int del_student(int fd, int id);
int compress_db(int fd);
void print_student(student_t *s);
int validate_range(int id, int gpa);
int count_db_records(int fd);
int print_db(int fd);
void usage(char *);
// error codes to be returned from individual functions
// NO_ERROR is returned if there are no errors
// ERR_DB_FILE is returned if there is are any issues with the database file itself
// ERR_DB_OP is returned if an operation did not work aka add or delete a student
// SRCH_NOT_FOUND is returned if the student is not found (get_student, and del_student)
#define NO_ERROR 0
#define ERR_DB_FILE -1
#define ERR_DB_OP -2
#define SRCH_NOT_FOUND -3
#define NOT_IMPLEMENTED_YET 0
// error codes to be returned to the shell
// EXIT_OK program executed without error
// EXIT_FAIL_DB a database operation failed
// EXIT_FAIL_ARGS one or more arguments to program were not valid
// EXIT_NOT_IMPL the operation has not been implemented yet
#define EXIT_OK 0
#define EXIT_FAIL_DB 1
#define EXIT_FAIL_ARGS 2
#define EXIT_NOT_IMPL 3
// Output messages
#define M_ERR_STD_RNG "Cant add student, either ID or GPA out of allowable range!\n"
#define M_ERR_DB_CREATE "Error creating DB file, exiting!\n"
#define M_ERR_DB_OPEN "Error opening DB file, exiting!\n"
#define M_ERR_DB_READ "Error reading DB file, exiting!\n"
#define M_ERR_DB_WRITE "Error writing DB file, exiting!\n"
#define M_ERR_DB_ADD_DUP "Cant add student with ID=%d, already exists in db.\n"
#define M_ERR_STD_PRINT "Cant print student. Student is NULL or ID is zero\n"
#define M_STD_ADDED "Student %d added to database.\n"
#define M_STD_DEL_MSG "Student %d was deleted from database.\n"
#define M_STD_NOT_FND_MSG "Student %d was not found in database.\n"
#define M_DB_COMPRESSED_OK "Database successfully compressed!\n"
#define M_DB_ZERO_OK "All database records removed!\n"
#define M_DB_EMPTY "Database contains no student records.\n"
#define M_DB_RECORD_CNT "Database contains %d student record(s).\n"
#define M_NOT_IMPL "The requested operation is not implemented yet!\n"
// useful format strings for print students
// For example to print the header in the required output:
// printf(STUDENT_PRINT_HDR_STRING, "ID","FIRST NAME",
// "LAST_NAME", "GPA");
#define STUDENT_PRINT_HDR_STRING "%-6s %-24s %-32s %-3s\n"
#define STUDENT_PRINT_FMT_STRING "%-6d %-24.24s %-32.32s %-3.2f\n"
#endif
\ No newline at end of file
#!/usr/bin/env bats
# The setup function runs before every test
setup_file() {
# Delete the student.db file if it exists
if [ -f "student.db" ]; then
rm "student.db"
fi
}
@test "Check if database is empty to start" {
run ./sdbsc -p
[ "$status" -eq 0 ]
[ "$output" = "Database contains no student records." ]
}
@test "Add a student 1 to db" {
run ./sdbsc -a 1 john doe 345
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Student 1 added to database." ]
}
@test "Add more students to db" {
run ./sdbsc -a 3 jane doe 390
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Student 3 added to database." ] || {
echo "Failed Output: $output"
return 1
}
run ./sdbsc -a 63 jim doe 285
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Student 63 added to database." ] || {
echo "Failed Output: $output"
return 1
}
run ./sdbsc -a 64 janet doe 310
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Student 64 added to database." ] || {
echo "Failed Output: $output"
return 1
}
run ./sdbsc -a 99999 big dude 205
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Student 99999 added to database." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Check student count" {
run ./sdbsc -c
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Database contains 5 student record(s)." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Make sure adding duplicate student fails" {
run ./sdbsc -a 63 dup student 300
[ "$status" -eq 1 ] || {
echo "Expecting status of 1, got: $status"
return 1
}
[ "${lines[0]}" = "Cant add student with ID=63, already exists in db." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Make sure the file size is correct at this time" {
run stat --format="%s" ./student.db
[ "$status" -eq 0 ]
[ "${lines[0]}" = "6400000" ] || {
echo "Failed Output: $output"
echo "Expected: 64000000"
return 1
}
}
@test "Find student 3 in db" {
run ./sdbsc -f 3
# Ensure the command ran successfully
[ "$status" -eq 0 ]
# Use echo with -n to avoid adding extra newline and normalize spaces
normalized_output=$(echo -n "${lines[1]}" | tr -s '[:space:]' ' ')
# Define the expected output
expected_output="3 jane doe 3.90"
# Compare the normalized output with the expected output
[ "$normalized_output" = "$expected_output" ] || {
echo "Failed Output: $normalized_output"
echo "Expected: $expected_output"
return 1
}
}
@test "Try looking up non-existent student" {
run ./sdbsc -f 4
[ "$status" -eq 1 ] || {
echo "Expecting status of 1, got: $status"
return 1
}
[ "${lines[0]}" = "Student 4 was not found in database." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Delete student 64 in db" {
run ./sdbsc -d 64
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Student 64 was deleted from database." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Try deleting non-existent student" {
run ./sdbsc -d 65
[ "$status" -eq 1 ] || {
echo "Expecting status of 1, got: $status"
return 1
}
[ "${lines[0]}" = "Student 65 was not found in database." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Check student count again, should be 4 now" {
run ./sdbsc -c
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Database contains 4 student record(s)." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Print student records" {
# Run the command
run ./sdbsc -p
# Ensure the command ran successfully
[ "$status" -eq 0 ]
# Normalize the output by replacing multiple spaces with a single space
normalized_output=$(echo -n "$output" | tr -s '[:space:]' ' ')
# Define the expected output (normalized)
expected_output="ID FIRST_NAME LAST_NAME GPA 1 john doe 3.45 3 jane doe 3.90 63 jim doe 2.85 99999 big dude 2.05"
# Compare the normalized output
[ "$normalized_output" = "$expected_output" ] || {
echo "Failed Output: $normalized_output"
echo "Expected Output: $expected_output"
return 1
}
}
@test "Compress db - try 1" {
skip
run ./sdbsc -x
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Database successfully compressed!" ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Delete student 99999 in db" {
run ./sdbsc -d 99999
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Student 99999 was deleted from database." ] || {
echo "Failed Output: $output"
return 1
}
}
@test "Compress db again - try 2" {
run ./sdbsc -x
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Database successfully compressed!" ] || {
echo "Failed Output: $output"
return 1
}
}
\ No newline at end of file
#! /bin/bash
./sdbsc -a 1 john doe 3.45
./sdbsc -a 3 jane doe 3.90
./sdbsc -a 63 jim doe 2.85
./sdbsc -a 64 janet doe 3.10
./sdbsc -a 99999 big dude 2.05
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment