Lecture 2 - Abstraction
Contents
Abstraction
Abstraction is the concept of taking a step back from something, and providing a wider overview.
It does NOT mean to be vague, but rather give a higher level of meaning.
Example
If we swap two variables withswap(&var1, &var2)
,
the user doesn’t need to know about thetemp
variable that is needed.
How to implement this, abstraction thing..?
There are two parts to an ADT, the interface, and the implementation
When designing a library,
What are the abstract properties of the datatypes we want to provide?
Interface
An ADT’s interface is the components that are exposed to the user to see and use. These are usually placed in an ADT’s file header (.h file).
Example
Function prototypes:void swap((void*)var1, (void*)var2)
Structure pointers:typedef struct st *St
Fun Fact: The C language allows us to forward-declare function and types - We term these as prototypes
Implementation
An ADT’s implementation is the actual inner-workings of the ADT… Helper functions, variable states, etcetera
Uses of Abstract Data Types
Stacks and Queues are common types of ADT implementations.
Stacks (LIFO)
The Last In is the First Out
Interface
The interfaces would be the functions that a program would use
Stack stack_new(item_t)
void stack_push(Stack, item_t)
item_t stack_pop(Stack)
size_t stack_size(Stack)
item_t stack_peek(Stack)
Implementation
|
|
Queues (FIFO)
The First In is the First Out
Interface
- push
- pop
- size
- peek
Implementation
code for queue functions go here!!
Analysis and Testing
It’s important to test our code… to make sure that it works.
> It’s also important to check for the efficiency of our code… more on that in the next lecture.
Approaches
TDD - Test Driven Development
Write tests for your code with expected values BEFORE you write your code.
(Often people write their code, then test it against output values… this isn’t writing tests, as all of your tests will be false negatives)
Unit Tests
Testing each function
Integration Tests
Testing functions together
Regression Tests
Testing that further development has not affected previous behaviour
Black Box / White Box Tests
There’s also another two types of testing.
Black Box Tests as aptly named - you don’t know what’s inside the black-box, only the inputs and outputs. For these tests, we compared the output given an input.
White Box Tests on the other hand, is testing with knowledge of the inner workings of the code. Here we’d be checking for structure values, counters, pointer locations, etcetera.
The lecturer wrote some stuff about Black and White Box Tests