Operating Systems TestAndSet instruction

I was reading operating systems concurrency control topics where I came across TestAndSet() intruction. I first referred this book. But it has got very less coverage on TestAndSet and then I googled. Obviously I came across wiki page.

The TestAndSet() function

#define LOCKED 1

int TestAndSet(int* lockPtr)
{
int oldValue;
oldValue = *lockPtr;
*lockPtr = LOCKED;
return oldValue;
}


Lock() built using TestAndSet()

function Lock(boolean *lock)
{
while (test_and_set(lock) == 1);
}


I have two doubts:

• It seems that the TestAndSet() does not actually have test, it does contain only set part and Lock() contains the set part. Is it like that?
Does that mean we can call TestAnsSet() inside Lock(), save its return value and test against that return value anytime inside Lock()?
You are right, TestAndSet doesn't test the value of the flag, it merely sets it, but returns the previous value, which must be tested elsewhere.